PDF Scraping with Templated Document

I cannot scrape other details from PDF File. Some document scraped all, while others are not. And this is the following issue I am encountering.

I am scraping a Sample PDF File.

CASE1:
Definition and Class Characteristic should be in one excel cell under Job Description column, BUT. Sometimes, it could be Definition and Class Characteristics or Definition and Distinguishing Characteristics or it could be `Definition and Class Characteristic AND Distinguishing Characteristics

CASE2:
Python is case sensitive, other document only uses Distinguishing CharacteristicS and other Job Description file does not haveS in the Characteristic word. How can we handle that?

CASE3:
EXAMPLE OF DUTIES should also be in one excel cell.

CASE4:
Education and Experience column should be a combination of Knowledge of and Ability to and sometimes Education and Training

Please see my code below:I hope any once could help me on this.

import fitz  # PyMuPDF
import pandas as pd
import re
import os


# Step 1: Extract Text from PDF
def extract_text_from_pdf(pdf_path):
    document = fitz.open(pdf_path)
    pdf_text = ""

    for page_num in range(len(document)):
        page = document.load_page(page_num)
        pdf_text += page.get_text()

    return pdf_text


# Define regular expression patterns with case insensitivity and optional character matching
job_title_re = re.compile(r'UNIT:s*(.*?)s*Class Specification', re.DOTALL | re.IGNORECASE)
class_specification_re = re.compile(r'Class Specifications*(.*?)s*DEFINITIONs*(.*?)s', re.DOTALL | re.IGNORECASE)
definition_re = re.compile(r'DEFINITIONs*(.*?)s*CLASS CHARACTERISTICS', re.DOTALL | re.IGNORECASE)
definition1_re = re.compile(r'DEFINITIONs*(.*?)s*DISTINGUISHING CHARACTERISTICS', re.DOTALL | re.IGNORECASE)
class_characteristics_re = re.compile(r'CLASS CHARACTERISTICSs*(.*?)s*EXAMPLE OF DUTIES', re.DOTALL | re.IGNORECASE)
distinguishing_characteristics_re = re.compile(r'DISTINGUISHING CHARACTERISTICSs*(.*?)s*EXAMPLE OF DUTIES', re.DOTALL | re.IGNORECASE)
example_of_duties_re = re.compile(r'EXAMPLE OF DUTIESs*(.*?)s*MINIMUM QUALIFICATIONS', re.DOTALL | re.IGNORECASE)
knowledge_of_re = re.compile(r'Knowledge of:s*(.*?)s*Ability to:', re.DOTALL | re.IGNORECASE)
ability_to_re = re.compile(r'Ability to:s*(.*?)s*Experience and Training', re.DOTALL | re.IGNORECASE)
experience_and_training_re = re.compile(r'Experience and Trainings*(.*?)s*WORKING CONDITIONS', re.DOTALL | re.IGNORECASE)


def extract_section(text, pattern):
    match = pattern.search(text)
    return match.group(1).strip() if match else ""


def process_pdfs_in_folder(pdf_folder, excel_path):
    all_data = []

    for pdf_file in os.listdir(pdf_folder):
        if pdf_file.endswith('.pdf'):
            pdf_path = os.path.join(pdf_folder, pdf_file)
            pdf_text = extract_text_from_pdf(pdf_path)

            # Extract sections using regex patterns
            job_title = extract_section(pdf_text, job_title_re)
            class_specification = extract_section(pdf_text, class_specification_re)
            definition = extract_section(pdf_text, definition_re)
            definition1 = extract_section(pdf_text, definition1_re)
            class_characteristics = extract_section(pdf_text, class_characteristics_re)
            distinguishing_characteristics = extract_section(pdf_text, distinguishing_characteristics_re)
            example_of_duties = extract_section(pdf_text, example_of_duties_re)
            knowledge_of = extract_section(pdf_text, knowledge_of_re)
            ability_to = extract_section(pdf_text, ability_to_re)
            experience_and_training = extract_section(pdf_text, experience_and_training_re)

            # Prepare data for the DataFrame
            data = {
                "Job Title": job_title + "n" + class_specification,
                "Definition and Class Characteristics": definition + "n" + definition1 + "n" + class_characteristics + "n" + distinguishing_characteristics,
                "Example of Duties": example_of_duties,
                "Knowledge, Ability, Experience, and Training": knowledge_of + "n" + ability_to + "n" + experience_and_training
            }

            all_data.append(data)

    # Create DataFrame from all data
    new_df = pd.DataFrame(all_data)

    # Check if the Excel file exists
    if os.path.exists(excel_path):
        existing_df = pd.read_excel(excel_path)
        final_df = pd.concat([existing_df, new_df], ignore_index=True)
    else:
        final_df = new_df

    # Write to Excel file
    final_df.to_excel(excel_path, index=False)
    print(f"Data has been written to {excel_path}")


# Define the paths
pdf_folder = r'C:UsersdonnaPycharmProjectsscrapePdfpythonProject.venvOceansideCAJobTitle'
excel_path = r'C:UsersdonnaPycharmProjectsscrapePdfpythonProject.venvOceansideCAJD_Excel.xlsx'

# Run the script
process_pdfs_in_folder(pdf_folder, excel_path)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật