Python 3.13 How to check how many pages a PDF file has
In order to do this, you need the Python module PyPDF2.
You install it with
pip3 install PyPDF2
Then it is important for this code here, you have the version
PyPDF2 Version 3.0.1
If not then you have to update the module because the programmer of the tool has changed a command within the module
PdfFileReader
➔PdfReader
In the foreign version the command PdfFileReader was the correct command but now in version 3.0.1 the command PdfReader is the correct one, otherwise this script does not work.
You check whether there is a new version of your module with this command:
pip3 list –outdated
You update the module with this command:
pip3 install –upgrade PyPDF2
Now the script:
from PyPDF2 import PdfReader
reader = PdfReader("PDF/Nachrichten/2024/Anfang_Dezember_2024.pdf")
totalPages = len(reader.pages)
print(f"The pdf has {totalPages} pages")
You see that the script also works not in the current file location.
My own video to this script, in my own YouTube channel: