#๐ how can i filter out the lines in a pdf
26 messages ยท Page 1 of 1 (latest)
@lucid gull
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.
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
i think pdf has no structure
yeah that is the problem :/
They're usually unstructured, yes.
Are you able to confirm that the PDF itself is the issue and not your text processing? How was the PDF produced? What did you expect as a result and what do you get instead?
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
Oh, I meant, do you know what application made it?
no clue ๐
that is just a single line.
yeah but adds the other lines
like the pdf one is a single line of the paragraph itself
pypdf2 is deprecated. the new package is called pypdf.
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 ๐
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)
I've tried it with a sample document and it seems to work with both pypdf and the older pypdf2 (you want pypdf though), so your text processing is probably fine. Yeah, I'd look into the lines themselves before you try to transform them.
i mean can do it cursed and set the lettertype to 2 so it fits on 1 line ๐ ๐
!close
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.