Python 3.13 How to check how many pages a PDF file has

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

Then it is important for this code here, you have the version

If not then you have to update the module because the programmer of the tool has changed a command within the module

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:

You update the module with this command:

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:

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

− 1 = 2