|
import gradio as gr |
|
import os |
|
import pandas as pd |
|
import pdfplumber |
|
import re |
|
import fitz |
|
import json |
|
|
|
""" |
|
Extract the text from a section of a PDF file between 'wanted_section' and 'next_section'. |
|
Parameters: |
|
- path (str): The file path to the PDF file. |
|
- wanted_section (str): The section to start extracting text from. |
|
- next_section (str): The section to stop extracting text at. |
|
Returns: |
|
- text (str): The extracted text from the specified section range. |
|
""" |
|
|
|
|
|
def get_section(path, wanted_section, next_section): |
|
print(wanted_section) |
|
|
|
|
|
doc = pdfplumber.open(io.BytesIO(path)) |
|
start_page = [] |
|
end_page = [] |
|
|
|
|
|
for page in range(len(doc.pages)): |
|
if len(doc.pages[page].search(wanted_section, return_chars=False, case=False)) > 0: |
|
start_page.append(page) |
|
if len(doc.pages[page].search(next_section, return_chars=False, case=False)) > 0: |
|
end_page.append(page) |
|
print(max(start_page)) |
|
print(max(end_page)) |
|
|
|
|
|
text = [] |
|
for page_num in range(max(start_page), max(end_page)+1): |
|
page = doc.pages[page_num] |
|
text.append(page.extract_text()) |
|
text = " ".join(text) |
|
new_text = text.replace("\n", " ") |
|
special_char_unicode_list = ["\u00e4", "\u00f6", "\u00fc", "\u00df"] |
|
special_char_replacement_list = ["ae", "oe", "ue", "ss"] |
|
for index, special_char in enumerate(special_char_unicode_list): |
|
final_text = new_text.replace(special_char, special_char_replacement_list[index]) |
|
return final_text |
|
|
|
|
|
def extract_between(big_string, start_string, end_string): |
|
|
|
pattern = re.escape(start_string) + '(.*?)' + re.escape(end_string) |
|
match = re.search(pattern, big_string, re.DOTALL) |
|
|
|
if match: |
|
|
|
return match.group(1) |
|
else: |
|
|
|
return None |
|
|
|
def format_section1(section1_text): |
|
result_section1_dict = {} |
|
|
|
result_section1_dict['TOPIC'] = extract_between(section1_text, "Sektor", "EZ-Programm") |
|
result_section1_dict['PROGRAM'] = extract_between(section1_text, "Sektor", "EZ-Programm") |
|
result_section1_dict['PROJECT DESCRIPTION'] = extract_between(section1_text, "EZ-Programmziel", "Datum der letzten BE") |
|
result_section1_dict['PROJECT NAME'] = extract_between(section1_text, "Modul", "Modulziel") |
|
result_section1_dict['OBJECTIVE'] = extract_between(section1_text, "Modulziel", "Berichtszeitraum") |
|
result_section1_dict['PROGRESS'] = extract_between(section1_text, "Zielerreichung des Moduls", "Massnahme im Zeitplan") |
|
result_section1_dict['STATUS'] = extract_between(section1_text, "Massnahme im Zeitplan", "Risikoeinschätzung") |
|
result_section1_dict['RECOMMENDATIONS'] = extract_between(section1_text, "Vorschläge zur Modulanpas-", "Voraussichtliche") |
|
|
|
return result_section1_dict |
|
|
|
|
|
def process_pdf(path): |
|
results_dict = {} |
|
results_dict["1. Kurzbeschreibung"] = \ |
|
get_section(path, "1. Kurzbeschreibung", "2. Einordnung des Moduls") |
|
""" |
|
results_dict["2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm"] = \ |
|
get_section(path, "2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm", |
|
"2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls") |
|
results_dict["2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm"] = \ |
|
get_section(path, "2.1 Aktualisierte Einordnung des Moduls in das EZ-Programm", |
|
"2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls") |
|
results_dict["2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls"] = \ |
|
get_section(path, "2.2 Andere Entwicklungsmaßnahmen im konkreten Interventionsbereich des Moduls", |
|
"3. Entwicklungen im Interventionsbereich") |
|
results_dict["3. Entwicklungen im Interventionsbereich"] = \ |
|
get_section(path, "3. Entwicklungen im Interventionsbereich", |
|
"4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren") |
|
results_dict["4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren"] = \ |
|
get_section(path, "4.1 Bewertungen von Zielen, Zielgruppen, Wirkungshypothesen und Indikatoren", |
|
"4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums") |
|
results_dict["4.2 Umgesetzte Maßnahmen / Aktivitäten während des Berichtszeitraums"] = \ |
|
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") |
|
results_dict["4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit des Vorhabens"] = \ |
|
get_section(path, "4.3 Umsetzung von Maßnahmen zur Sicherstellung der nachhaltigen Wirksamkeit", |
|
"4.4 Laufzeit und Zeitplan") |
|
results_dict["4.4 Laufzeit und Zeitplan"] = \ |
|
get_section(path, "4.4 Laufzeit und Zeitplan", "4.5 Entstandene Kosten und Kostenverschiebungen") |
|
results_dict["4.5 Entstandene Kosten und Kostenverschiebungen"] = \ |
|
get_section(path, "4.5 Entstandene Kosten und Kostenverschiebungen", "4.6 Bewertung der Wirkungen und Risiken") |
|
results_dict["4.6 Bewertung der Wirkungen und Risiken"] = \ |
|
get_section(path, "4.6 Bewertung der Wirkungen und Risiken", "5. Übergeordnete Empfehlungen") |
|
results_dict["5.1 Empfehlungen und Merkposten für den Politik- und Schwerpunktdialog"] = \ |
|
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") |
|
results_dict[ |
|
"5.2 Lernerfahrungen, die für die Länderstrategie und zukünftige EZ-Programme interessant sein könnten"] = \ |
|
get_section(path, "5.2 Lernerfahrungen", "6. Testat") |
|
results_dict["6. Testat (TZ)"] = \ |
|
get_section(path, "6. Testat", "Anlage 1: Wirkungsmatrix des Moduls") |
|
""" |
|
|
|
result_section1_dict = format_section1(results_dict.get("1. Kurzbeschreibung")) |
|
|
|
|
|
""" |
|
def get_first_page_text(path): |
|
doc = pdfplumber.open(io.BytesIO(path)) |
|
if len(doc.pages): |
|
return doc.pages[0].extract_text() |
|
""" |
|
|
|
|
|
|
|
iface = gr.Interface(fn=process_pdf, |
|
inputs=gr.File(type="binary", label="Upload PDF"), |
|
outputs=gr.Textbox(label="Extracted Text"), |
|
title="PDF Text Extractor", |
|
description="Upload a PDF file to extract all its text.") |
|
|
|
iface.launch() |