andreeabodea commited on
Commit
0b16387
·
verified ·
1 Parent(s): c158ccc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import pandas as pd
4
+ import pdfplumber
5
+ import re
6
+ import fitz # PyMuPDF
7
+ import json
8
+
9
+ files = [f for f in os.listdir("/Users/andreeabodea/") if f.endswith(".pdf")]
10
+ print(files)
11
+
12
+ """
13
+ Extract the text from a section of a PDF file between 'wanted_section' and 'next_section'.
14
+ Parameters:
15
+ - path (str): The file path to the PDF file.
16
+ - wanted_section (str): The section to start extracting text from.
17
+ - next_section (str): The section to stop extracting text at.
18
+ Returns:
19
+ - text (str): The extracted text from the specified section range.
20
+ """
21
+ def get_section(path, wanted_section, next_section):
22
+ print(wanted_section)
23
+
24
+ # Open the PDF file
25
+ doc = pdfplumber.open(path)
26
+ start_page = []
27
+ end_page = []
28
+
29
+ # Find the all the pages for the specified sections
30
+ for page in range(len(doc.pages)):
31
+ if len(doc.pages[page].search(wanted_section, return_chars = False, case = False)) > 0:
32
+ start_page.append(page)
33
+ if len(doc.pages[page].search(next_section, return_chars = False, case = False)) > 0:
34
+ end_page.append(page)
35
+ print(max(start_page))
36
+ print(max(end_page))
37
+
38
+ # Extract the text between the start and end page of the wanted section
39
+ text = []
40
+ for page_num in range(max(start_page), max(end_page)):
41
+ page = doc.pages[page_num]
42
+ text.append(page.extract_text())
43
+ text = " ".join(text)
44
+ new_text = text.replace("\n", " ")
45
+ special_char_unicode_list = ["\u00e4", "\u00f6", "\u00fc", "\u00df"]
46
+ special_char_replacement_list = ["ae", "oe", "ue", "ss"]
47
+ for index, special_char in enumerate(special_char_unicode_list):
48
+ final_text = new_text.replace(special_char, special_char_replacement_list[index])
49
+ return final_text
50
+
51
+ for file in files:
52
+
53
+ print("for each pdf file...")
54
+ path = "/Users/andreeabodea/" + file
55
+ pdf = pdfplumber.open(path)
56
+ print(path)
57
+
58
+ results_dict = {}
59
+ results_dict["2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm"] = \
60
+ get_section(path, "2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm", "2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls")
61
+ results_dict["2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm"] = \
62
+ get_section(path,"2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm", "2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls")
63
+ results_dict["2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls"] = \
64
+ get_section(path, "2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls", "3. Entwicklungen im Interventionsbereich")
65
+ results_dict["3. Entwicklungen im Interventionsbereich"] = \
66
+ get_section(path, "3. Entwicklungen im Interventionsbereich", "4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren")
67
+ results_dict["4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren"] = \
68
+ get_section(path, "4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren", "4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums")
69
+ results_dict["4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums"] = \
70
+ get_section(path, "4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums", "4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit")
71
+ results_dict["4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit des Vorhabens"] = \
72
+ get_section(path, "4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit", "4.4 Laufzeit und Zeitplan")
73
+ results_dict["4.4 Laufzeit und Zeitplan"] = \
74
+ get_section(path, "4.4 Laufzeit und Zeitplan", "4.5 Entstandene Kosten und Kostenverschiebungen")
75
+ results_dict["4.5 Entstandene Kosten und Kostenverschiebungen"] = \
76
+ get_section(path, "4.5 Entstandene Kosten und Kostenverschiebungen", "4.6 Bewertung der Wirkungen und Risiken")
77
+ results_dict["4.6 Bewertung der Wirkungen und Risiken"] = \
78
+ get_section(path, "4.6 Bewertung der Wirkungen und Risiken", "5. Übergeordnete Empfehlungen")
79
+ results_dict["5.1 Empfehlungen und Merkposten für den Politik- und Schwerpunktdialog"] = \
80
+ get_section(path, "5.1 Empfehlungen und Merkposten für den Politik- und Schwerpunktdialog", "5.2 Lernerfahrungen, die für die Länderstrategie und zukünftige EZ-Programme")
81
+ results_dict["5.2 Lernerfahrungen, die für die Länderstrategie und zukünftige EZ-Programme interessant sein könnten"] = \
82
+ get_section(path, "5.2 Lernerfahrungen", "6. Testat")
83
+ results_dict["6. Testat (TZ)"] = \
84
+ get_section(path, "6. Testat", "Anlage 1: Wirkungsmatrix des Moduls")
85
+
86
+ print(results_dict)
87
+
88
+ #json_string = json.dumps(results_dict, indent=4)
89
+ #print(json_string)
90
+
91
+ # iface = gr.Interface(fn=get_section, inputs="text", outputs="text")
92
+ # iface.launch()