-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exceptions / missing spaces in extract_text() method #17
Comments
Hello, Works for me, but the extracted text contains no spaces :/ input = PdfFileReader(open("foo.pdf", 'rb'))
print input.getPage(0).extractText() Is that a known issue ? |
Hmm to make it more clear, the issue seem to appear for 2 columns papers, this one for example: |
The extractText method is probably a little crude, and definitely doesn't function well for PDFs with complicated text. It could use some work to return text in a more orderly fashion that more closely appears like the text you see in a PDF viewer. |
Another pdf where whitespace is not preserved in extracted text |
I tried to extract arabic text out of a PDF file using extractText() method. However, arabic text disappears in the output. |
To resolve the problem of missing whitespaces, I propose the following for-loop in the extractText method. The part below “text += i” is new. The limit “i < -100” where a spacing becomes a whitespace is arbitrarily chosen; in a typical Springer pdf book a value of -300 to -200 determines a whitespace. Although this may look like a hack, I can think of no other criterion for a whitespace in such documents.
|
@woho's idea worked for me.
|
Some PDFs apparently generate empty operands. If this condition is explicitly checked, then I can avoid some thrown exceptions:
Quick fix:
|
There should be a newline somewhere:
|
It seems that the value of the Float/NumberObject directly encodes the distance between two pieces of text, with the width of one space equaling -600:
|
A lot of the whitespace issues got fixed via #569 |
#924 Improved further on the whitespace issue |
I think it is fixed. Minimal examplefrom PyPDF2 import PdfReader
reader = PdfReader("vollmer_fischer.pdf") # www.rowland.harvard.edu/rjf/vollmer/images/vollmer_fischer.pdf
text = reader.pages[0].extract_text() text now is:
|
The highlight of the 2.1.0 release is the most massive improvement to the text extraction capabilities of PyPDF2 since 2016 🥳🎊 A very big thank you goes to [pubpub-zz](https://github.com/pubpub-zz) who took a lot of time and knowledge about the PDF format to finally get those improvements into PyPDF2. Thank you 🤗💚 In case the new function causes any issues, you can use `_extract_text_old` for the old functionality. Please also open a bug ticket in that case. There were several people who have attempted to bring similar improvements to PyPDF2. All of those were valuable. The main reason why they didn't get merged is the big amount of open PRs / issues. pubpub-zz was the most comprehensive PR which also incorporated the latest changes of PyPDF2 2.0.0. Thank you to [VictorCarlquist](https://github.com/VictorCarlquist) for #858 and [asabramo](https://github.com/asabramo) for #464 🤗 New Features (ENH): - Massive text extraction improvement (#924). Closed many open issues: - Exceptions / missing spaces in extract_text() method (#17) 🕺 - Whitespace issues in extract_text() (#42) 💃 - pypdf2 reads the hifenated words in a new line (#246) - PyPDF2 failing to read unicode character (#37) - Unable to read bullets (#230) - ExtractText yields nothing for apparently good PDF (#168) 🎉 - Encoding issue in extract_text() (#235) - extractText() doesn't work on Chinese PDF (#252) - encoding error (#260) - Trouble with apostophes in names in text "O'Doul" (#384) - extract_text works for some PDF files, but not the others (#437) - Euro sign not being recognized by extractText (#443) - Failed extracting text from French texts (#524) - extract_text doesn't extract ligatures correctly (#598) - reading spanish text - mark convert issue (#635) - Read PDF changed from text to random symbols (#654) - .extractText() reads / as 1. (#789) - Update glyphlist (#947) - inspired by #464 - Allow adding PageRange objects (#948) Bug Fixes (BUG): - Delete .python-version file (#944) - Compare StreamObject.decoded_self with None (#931) Robustness (ROB): - Fix some conversion errors on non conform PDF (#932) Documentation (DOC): - Elaborate on PDF text extraction difficulties (#939) - Add logo (#942) - rotate vs Transformation().rotate (#937) - Example how to use PyPDF2 with AWS S3 (#938) - How to deprecate (#930) - Fix typos on robustness page (#935) - Remove scripts (pdfcat) from docs (#934) Developer Experience (DEV): - Ignore .python-version file - Mark deprecated code with no-cover (#943) - Automatically create Github releases from tags (#870) Testing (TST): - Text extraction for non-latin alphabets (#954) - Ignore PdfReadWarning in benchmark (#949) - writer.remove_text (#946) - Add test for Tree and _security (#945) Code Style (STY): - black, isort, Flake8, splitting buildCharMap (#950) Full Changelog: 2.0.0...2.1.0
extractText() method isn't broken, but throws some exceptions in these cases:
http://doctor12wer.blogspot.com/2013/06/extracttext-function-in-pypdf2-throws.html
http://stackoverflow.com/questions/17270387/pypdf2-typeerror-when-trying-to-extract-text
The text was updated successfully, but these errors were encountered: