#๐Ÿ”’ how can i filter out the lines in a pdf

26 messages ยท Page 1 of 1 (latest)

lucid gull
#

like for docx files it works just hate the pdf format lol but gotta use it :/

covert pulsarBOT
#

@lucid gull

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lucid gull
#
from docx import Document
import PyPDF2
import re

def extract_text_from_docx(file_path):
    doc = Document(file_path)
    text = []
    for paragraph in doc.paragraphs:
        text.append(paragraph.text)
    return "\n".join(text)

def extract_text_from_pdf(file_path):
    with open(file_path, 'rb') as file:
        reader = PyPDF2.PdfReader(file)
        text = []
        for page in reader.pages:
            text.append(page.extract_text())
    return "\n".join(text)

def filter_lines_with_caret(text):
    lines = text.splitlines()
    results = []
    for line in lines:
        line = line.strip()
        if line.startswith('^'):
            match_with_equals = re.match(r'^\^ *(\d+)\s*=', line)
            if match_with_equals:
                results.append(match_with_equals.group(1))
            else:
                cleaned_line = re.sub(r'^\^ *', '', line)
                results.append(cleaned_line)
    return "\n".join(results)


def extract_text(file_path):
    if file_path.endswith('.docx'):
        text = extract_text_from_docx(file_path)
    elif file_path.endswith('.pdf'):
        text = extract_text_from_pdf(file_path)
    else:
        raise ValueError("Unsupported file format. Please use a .docx or .pdf file.")

    return filter_lines_with_caret(text)

if __name__ == "__main__":
    file_path = input("Voer het pad naar het bestand in (.docx of .pdf): ").strip()
    try:
        text = extract_text(file_path)
        print("\nGevonden tekst zonder '^':\n")
        print(text)
    except Exception as e:
        print(f"Er is een fout opgetreden: {e}")
#

def filter_lines_with_caret(text):

#

is for this func just don t know how it can handle pdfs file without breaking the structure

icy geyser
#

i think pdf has no structure

lucid gull
#

yeah that is the problem :/

rose grail
lucid gull
#

this is with docx

#

and with pdf it will just take the single line and not add it to each other

#

How was the PDF produced? just making one?
What did you expect as a result and what do you get instead?
hope the pictures explain the results

rose grail
lucid gull
#

no clue ๐Ÿ˜…

icy geyser
lucid gull
#

like the pdf one is a single line of the paragraph itself

icy geyser
#

pypdf2 is deprecated. the new package is called pypdf.

lucid gull
#

okay thank you will keep that in mind will close the post for now and discuss it why my groupmembers ig thank you guys for the help ๐Ÿ˜Š

icy geyser
#

i still don't know what your problem is. but maybe the new pypdf version works better.

#

you always say that lines get added. but i think that's what you're trying to do. maybe it would make sense to look at the output of pypdf before you make all these transformations. (what does page.extract_text() return)

rose grail
lucid gull
#

i mean can do it cursed and set the lettertype to 2 so it fits on 1 line ๐Ÿ˜‚ ๐Ÿ˜ˆ

#

!close

covert pulsarBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.