Skip to content
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

Encoding issue in extract_text() #235

Closed
macabeus opened this issue Nov 13, 2015 · 2 comments
Closed

Encoding issue in extract_text() #235

macabeus opened this issue Nov 13, 2015 · 2 comments
Labels
workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow

Comments

@macabeus
Copy link

I need to read this PDF.
However, it does not correctly extracts the text.

f = open('myfile.pdf', 'rb')
reader = PdfFileReader(f)
content = reader.getPage(0).extractText()
f.close()

print(content)

This print

Resultado da Prova de Sele“‰o...

But I expected

Resultado da Prova de Seleção...

Accordance with the answer on Stack Overflow, this problem is in PyPDF

@macabeus
Copy link
Author

I did a workaround to fix this problem. I do not know what encoding is it, then I fix each character individually.

In file pdf.py, change the last elif for it:

elif operator == b_("TJ"):
    for i in operands[0]:
        if isinstance(i, TextStringObject):
            x = i.get_original_bytes()
            fix = ''
            for i in x:
                if i == 135:
                    fix += 'á'
                elif i == 137:
                    fix += 'â'
                elif i == 139:
                    fix += 'ã'
                elif i == 141:
                    fix += 'ç'
                elif i == 142:
                    fix += 'é'
                elif i == 144:
                    fix += 'ê'
                elif i == 146:
                    fix += 'í'
                elif i == 151:
                    fix += 'ó'
                elif i == 153:
                    fix += 'ô'
                elif i == 155:
                    fix += 'õ'
                elif i == 156:
                    fix += 'ú'
                elif i == 231:
                    fix += 'Á'
                elif i == 234:
                    fix += 'Í'
                else:
                    fix += chr(i)

                text += fix

@mstamy2 mstamy2 added the workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow label May 19, 2016
@MartinThoma
Copy link
Member

@macabeus I can't access that PDF, but I assume that #924 has fixed this issue. The release of it will be today (PyPDF2==2.1.0). Please ping me if that (didn't) solve the issue :-)

For the moment, I close it as I assume it's gone.

@MartinThoma MartinThoma changed the title Coding error when extracting text Encoding issue in extract_text() Jun 6, 2022
MartinThoma added a commit that referenced this issue Jun 6, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow
Projects
None yet
Development

No branches or pull requests

3 participants