I'm having trouble getting all the page numbers from a pdf file.
this is my code!
I just get a one-page number that outputs I'm trying to get all the page numbers from my pdf file. How would I fix my code to get all the pdf page numbers? In total there are 20 pages.
enter image description here
Asked
Active
Viewed 860 times
-1

Martin Thoma
- 124,992
- 159
- 614
- 958

George
- 1
- 1
-
3Please post code as text, not an image of text. – Scott Hunter Jul 14 '22 at 18:47
-
1also the error message... include it as text. [read this](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question) – Omkar76 Jul 14 '22 at 18:50
2 Answers
0
My attempt looks something like this:
import PyPDF2
pdffileobj = open('test.pdf','rb')
pdfreader = PyPDF2.PdfFileReader(pdffileobj)
#extract the number of pages in the pdf and all text from the pdf
data = ''
#extract the text from the pdf
for i in range(pdfreader.numPages):
pageobj = pdfreader.getPage(i)
data += pageobj.extractText()

Patrick White
- 11
- 2
0
See https://pypdf2.readthedocs.io/en/latest/user/extract-text.html
from PyPDF2 import PdfReader
reader = PdfReader("example.pdf")
for page in reader.pages:
print(page.extract_text())
print(f"pdf page count : {len(reader.pages)}")

Martin Thoma
- 124,992
- 159
- 614
- 958