Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyPDF2 or indentation issue
#1
Hello

i am trying to read each file from a directory, then read the text from the pdf file, however its giving me an error message, first below is the code:

from PyPDF2 import PdfFileReader, PdfFileWriter
import os

directory = os.listdir("C:\example")

for file in directory:
   if file.endswith(".pdf"):
    pfile = open("C:\example\\"+file,'rb')
	pdfFile = PdfFileReader(open(pfile)
	page = pdfFile.getPage(0)
	print(page.extractText())
and its giving me this error message:
Error:
C:\WSDL>Read_PDF.py File "C:\WSDL\Read_PDF.py", line 9 pdfFile = PdfFileReader(open(pfile) ^ TabError: inconsistent use of tabs and spaces in indentation
however if i change the code to this:

from PyPDF2 import PdfFileReader, PdfFileWriter
import os

directory = os.listdir("C:\example")

for file in directory:
   if file.endswith(".pdf"):
    pfile = open("C:\example\\"+file,'rb')
    print(pfile)
	#pdfFile = PdfFileReader(open(pfile)
	#page = pdfFile.getPage(0)
	#print(page.extractText())
this code works, so i am not sure if this PyPDF2 is having issues, i checked if it was installed and it is, i did a pip install PyPDF2 and it says its already installed.

so i am not sure if i am using PyPDF2 correclty? any help would be great.
Reply
#2
missing close parentheses line 9 as indicated in the error message.
Reply
#3
Also indentation always 4-space,never single \ that way in filename Windows.
You open file 2 times in first code.
from PyPDF2 import PdfFileReader, PdfFileWriter
import os

directory = os.listdir("E:/pdf/1")
for file in directory:
    if file.endswith(".pdf"):
        pfile = "E:/pdf/1/" + file
        pdfFile = PdfFileReader(open(pfile, 'rb'))
        page = pdfFile.getPage(0)
        print(page.extractText())
Reply
#4
big thanks Snippsat you are correct :)thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyPDF2 deprecation problem gowb0w 5 3,975 Sep-21-2023, 12:38 PM
Last Post: Pedroski55
  ModuleNotFoundError: No module named 'PyPDF2' Benitta2525 1 1,483 Aug-07-2023, 05:32 AM
Last Post: DPaul
  Pypdf2 will not find text standenman 2 934 Feb-03-2023, 10:52 PM
Last Post: standenman
  pyPDF2 PDFMerger close pensding file japo85 2 2,412 Jul-28-2022, 09:49 AM
Last Post: japo85
  PyPDF2 processing problem Pavel_47 6 9,748 May-04-2021, 06:58 AM
Last Post: chaitanya
Bug Python Shell 3.9.0 - Issue with indentation Earis 17 6,609 Oct-31-2020, 07:00 AM
Last Post: Earis
  Problem with installing PyPDF2 Pavel_47 2 6,018 Nov-10-2019, 02:58 PM
Last Post: Pavel_47
  looping and indentation issue ameydiwanji 3 2,436 Jul-01-2019, 10:53 AM
Last Post: perfringo
  pyPDF2 nautilus columns modification AJBek 1 2,900 Jun-07-2019, 04:17 PM
Last Post: micseydel
  Using Pypdf2 write a string to a pdf file Pedroski55 6 20,261 Apr-11-2019, 11:10 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020