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

PyPDF2 failing to read unicode character #37

Closed
SharmileeS opened this issue Nov 15, 2013 · 9 comments
Closed

PyPDF2 failing to read unicode character #37

SharmileeS opened this issue Nov 15, 2013 · 9 comments
Labels
workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow

Comments

@SharmileeS
Copy link

SharmileeS commented Nov 15, 2013

I have a PDF which PDFFileReader is unable to read the text , instead this is the output:

u'\n˘ˇˆ˘ˇ˙˝˛˛˚˜ !!"#$%&"˝˛˝˘˛˘˛˚˙˘ˇ˝˛˘˛$\'(˘%˘ˇ˘ˆ˘)_)˛\'+,-)"˛./0"0!123˛"4˙"5)46)!6"˙˘˘˘,˘ˇˆ˙˙ˆ˝˛˚˜ !˘ˇˆ˙˝"" ˜#˝$˛˚˜ ˆ˙˝"" ˜ %˛˚˜ !˛˚ˇ!"#$%˘ˇ&ˆ˙˝˛˝ˆ˙&˚˝\'˛˚&\'()_ˇ+˙˝"" ˜#˝$˜#( ˛˚(ˇ+,˘˘˘ˇˆˆˆˇ,ˆ--ˆˇˇ˙˝˝% ˜)˜#_#˝$$˜  ˙ ˝_˛˚ˆ-&ˆ!ˆˇ&˘+$ˆ(˙˝+˚˜,!˛˚./&0ˆˆ+$ˆ(˙˝-˛-,&˘˝ˆ. ˚%˝% ˜)˜#\* ˜!˛˚&ˆˇ%ˆ!&(12+3ˇ˙˝,˜ˆ/˛˚%#"+3("ˆˇ.!ˆˇ43ˇ(˙-,&53ˇ6ˆˇ,˝˝% ˜)˜#\* ˜!˛˚(77777777777˜#( 0123& ˜"" ˜ %˛˚˜ 77777777777˜#( _ˆ_˛ ,4+#(56˝% ˜)˜#\* ˜!7  56 _˜ˆ(  %!_ˆ_˛ ˆ˙&˚˝\'586"ˇ+((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((&\'()_&\'(_&\'()˘536((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((&\'&\' &\'˜ ˜˙˚ˆ-",ˇˆˇ!ˆ-ˆ,ˆ&ˆ!ˆˇ&53ˇ6ˆˇ,(˙˚&ˆ!-ˇ!6ˆˇ,˘ 8-ˇˆ-˙˝˝% ˜)˜#_ ˜!7  ˛˚(˙˚9ˇˇˆ-6ˆˇ,:;ˇˇˆ-<ˆˆ-ˇ&\' ,,˘˘ˇˇˆ-(9ˆˇˆ-!˘ˇˆ9˘ˆˇ˘˘(\n\n'

This is the output after Extract Text and it doesnot throw any error message.

A similar issue has been posted here:

http://stackoverflow.com/questions/15583535/how-to-extract-text-from-a-pdf-file-in-python
I am using windows so the solution in link is not helpful

@mstamy2
Copy link
Collaborator

mstamy2 commented Nov 19, 2013

This issue is larger than PyPDF2; the text of the PDF is likely not stored in a 'reasonably exportable way'. Can you successfully copy/paste text from the PDF as you view it in a PDF viewer? If not, and the text that gets pasted is unreadable or in a binary format, then the above is true.

The description here http://stackoverflow.com/questions/12703387/pdf-font-encoding
explains how most tools fail to extract text from PDFs such as this. Unfortunately, the options given in the answer on that page are rather limited.

If you can successfully copy/paste text from the PDF, then the above may not be the case.

@Rob1080
Copy link
Contributor

Rob1080 commented Dec 10, 2013

I think I have an issue related to this (if it doesn't seem related,please let me know and I'll create a new issue).
When writing a final doc (merged some pages from other pdfs) I get an error

UnicodeDecodeError: 'utf8' codec can't decode byte 0xae in position 13: invalid start byte

The it's caused by this: /ARX#20CoSign\xae'

The code where the issue occurs is in generic.py, line 471

return NameObject(name.decode('utf-8'))

To "fix" it, I changed the code to

    try:
            name = name.decode('utf-8')
   except Exception, e:
            pass

   return NameObject(name)

I know it's a hack, but it seemed to resolve the issue I was having. The pdf generates after making this change.

@caxap
Copy link
Contributor

caxap commented Mar 17, 2014

Hey @Rob1080, thanks for your fix. I have same problem with '/ARX#20CoSign\xae' name. Are you found any problems with this fix? Or is there a better solution?

Also I think this fix can be slightly improved:

    try:
        name = name.decode('utf-8')
    except UnicodeDecodeError:
        pass
    return NameObject(name)

@Rob1080
Copy link
Contributor

Rob1080 commented Mar 17, 2014

@caxap It's working fine so far. Fix was merged here: https://github.com/mstamy2/PyPDF2/pull/52/files

@caxap
Copy link
Contributor

caxap commented Mar 17, 2014

@Rob1080 thanks again. Updated to new version.

@hongquan
Copy link

This PDF is generated by xhtml2pdf 0.0.6, but is not read properly by PyPDF2 https://drive.google.com/file/d/0ByKUrmVuTXp5RndEVWYxYWM2dGc/view?usp=sharing

The Unicode text there is distorted. The u'Nguy\u1ec5n H\u1ed3ng Qu\xe2n' text is read as Nguy\u2026n H\u2014ng Qu\u2013n

@rahatUlAin
Copy link

I am also getting result like this while trying to read a pdf file with unicode urdu using pyPDF2 .It(text) get copied and pasted but the words get inverted like 'urdu' become 'udru'(this for your convenience other wise its more like یقین->نیقی )
\n\n\n\n\n \n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n2|14|\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\

@AzizieAbuduaini
Copy link

what i have tried is to replace specific " ' " unicode with "’" which solves this issue. Please let me know if u still failed to generate pdf with this approach.
text = text.replace("'", "’")

@MartinThoma MartinThoma added the workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow label Apr 16, 2022
@py-pdf py-pdf deleted a comment from TejasSutar01 Apr 21, 2022
@MartinThoma
Copy link
Member

We recently improved the situation a lot with #924 (release will be today). Without a specific PDF file, I cannot verify if this issue is solved. I assume it is. Please open another issue if that is not the case.

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

8 participants