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

extract_text works for some PDF files, but not the others #437

Closed
babak-khamsehi opened this issue Jun 22, 2018 · 20 comments
Closed

extract_text works for some PDF files, but not the others #437

babak-khamsehi opened this issue Jun 22, 2018 · 20 comments
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF needs-pdf The issue needs a PDF file to show the problem workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow

Comments

@babak-khamsehi
Copy link

babak-khamsehi commented Jun 22, 2018

I am using Python 3.6.1 on Windows 8.1 and I want to extract certain texts from a group of PDF files. To do so, I am using this code and it works fine returning the PDF as a continuous text as string variable:

import PyPDF2
# creating a pdf file object
pdfFileObj = open('C:/Google Drive/Ward 29/data/ndvi.pdf', 'rb')
# creating a pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj, strict=False)
# getting the number of pages in pdf file
number_of_pages =pdfReader.getNumPages()
# creating a page object
pageObj = pdfReader.getPage(0)
page_content = pageObj.extractText()
print(page_content)
# closing the pdf file object

However, print(page_content) does return null if I use another PDF file, “55 HARRISON GARDEN.pdf” which I actually need to extract some information from:

### This code works for the ndvi file, but returns empty string for the 
harrison gdn file! I need to figure out why
import PyPDF2
# creating a pdf file object
pdfFileObj = open('C:/Google Drive/Ward 29/data/55 HARRISON GARDEN.pdf', 
'rb')
# creating a pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj, strict=False)
# getting the number of pages in pdf file
number_of_pages =pdfReader.getNumPages()
# creating a page object
pageObj = pdfReader.getPage(0)
page_content = pageObj.extractText()
print(page_content)
# closing the pdf file object
pdfFileObj.close()

Can anyone help me figure how I can fix it to read that pdf, “55 Harrison Garden.pdf” as well?

Here are the files I mentioned: https://drive.google.com/drive/folders/1wzrPsPoeqZolsd7u0NS-I44sliFhxIpu?usp=sharing

@Yiidiir
Copy link

Yiidiir commented Jun 26, 2018

I had the same issue, i fixed it using another python library called slate
Fortunately, i found a fork that works in Python 3.6.5

import slate3k as slate

with open(file.pdf,'rb') as f:
    extracted_text = slate.PDF(f)
print(extracted_text)

@hafizurcse
Copy link

hafizurcse commented Apr 11, 2020

What I did is:

pip install slate3k

and then do same as suggested by @Yiirdiir

import slate3k as slate

with open('my-pdf-file-name.pdf', 'rb') as f:
    extracted_text = slate.PDF(f)
print(extracted_text)

@shivstha
Copy link

shivstha commented May 16, 2020

to extract all pages from pdf

import PyPDF2
pdfFileObj = open('your_pdf_name.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pdf = ''
for i in range(0, pdfReader.numPages):
    pageObj = pdfReader.getPage(i)
    page = pageObj.extractText()
    pdf = page + ' '
print(pdf)

@Akansha-SH
Copy link

how to get all the pages from pdf ?

@shivstha
Copy link

how to get all the pages from pdf ?

#437 (comment)

@kadnan
Copy link

kadnan commented Jul 12, 2020

pip install slate3k

I ran the sample code and it returned:

WARNING:pdfminer.layout:Too many boxes (245) to group, skipping.
WARNING:pdfminer.layout:Too many boxes (204) to group, skipping.

@ukumar009
Copy link

I had the same issue, i fixed it using another python library called slate
Fortunately, i found a fork that works in Python 3.6.5

import slate3k as slate

with open(file.pdf,'rb') as f:
    extracted_text = slate.PDF(f)
print(extracted_text)

Thank you @hafizurcse your solution worked well compared to pdf2reader extracting all data. thank you for sharing the solution.

@SohamDasBiswas
Copy link

SohamDasBiswas commented Nov 28, 2020

@UdayKumar-Ch, @Yiidiir, @hafizurcse

Sir, Does slate3k can count the number of pages on a pdf?

  1. @Yiidiir
  2. @hafizurcse
  3. @UdayKumar-Ch

@rashmiinamdar
Copy link

I tried with slate3k, but returned output as - ['\x0c']
Someone please help me

@Manjeete
Copy link

Manjeete commented Feb 1, 2021

I tried with slate3k, but returned output as - ['\x0c']
Someone please help me

same here

@Ndofi
Copy link

Ndofi commented Mar 15, 2021

How to extract multiple pdf files data in the same time?

@madhavmansuriya40
Copy link

I tried with slate3k, but returned output as - ['\x0c']
Someone please help me

@rashmiinamdar @Manjeete
image embedded PDF can be the reason for this output
please try with the original pdf (generated from the machine)

sample.pdf

@pgm-master
Copy link

But is it possible to extract specific text from the pdf?

@MartinThoma
Copy link
Member

Is this related to #168? Maybe even the same?

@MartinThoma MartinThoma added the is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF label Apr 8, 2022
@py-pdf py-pdf deleted a comment from Yiidiir Apr 16, 2022
@MartinThoma MartinThoma added the workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow label Apr 16, 2022
@MartinThoma
Copy link
Member

I don't know how to reproduce this issue. I guess #924 has fixed it.

I'll release PyPDF2==2.1.0 today. Please ping me if you have this issue again/still.

@MartinThoma MartinThoma changed the title PYPDF2 can extract text from some PDF files, but not the others… extract_text works for some PDF files, but not the others 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
@ajinkyaG28
Copy link

@MartinThoma I am still facing that issue(cant extract from some pdfs) with PyPDF==2.4.2.

@MartinThoma
Copy link
Member

Can you share such a PDF?

@MartinThoma MartinThoma added the needs-pdf The issue needs a PDF file to show the problem label Jul 7, 2022
@ajinkyaG28
Copy link

@MartinThoma Cant share work related pdf, but found this example of scanned pdf online: http://solutions.weblite.ca/pdfocrx/scansmpl.pdf
Cant extract from this pdf as well. Does PyPDF work with scanned pdfs?

@MartinThoma
Copy link
Member

No. PyPDF2 does not do ocr. This is not a bug, but simply out of scope.

@MartinThoma
Copy link
Member

But it might be worth documenting as I realize that the difference is not clear 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
is-bug From a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF needs-pdf The issue needs a PDF file to show the problem workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow
Projects
None yet
Development

No branches or pull requests