Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Where is the wrong indentation?

I have the following code. I don't know how PDFminer works so the first part is somebody else code which I modified a bit. It seems to work but does everything 8 or more times instead of just one and gets slower with every line I add. The point of it would be that there are a lot of PDF-s in a folder. I want to open some which have specific words in their titles and extract some words into an excel file. The concept seems to work. The problem is what i described above. I think there is a wring indentation. Or is it something else? I am still learning. Thank you for your help.
I also had to change the variables to fruits, I hope it doesn't look that silly. It is far from finished so most fruits are not added in the end yet.

import glob, os
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from io import StringIO #first was cStringIO
import re

filename = "Data_Aquisition.csv"
f = open(filename, "a", encoding="utf-8")
headers = "company_name, apple_date, apple_designation, banana_date, banana_Designation, orange_date, orange_designation, pear, cherry_date, cheery_company\n"

pdflist = glob.glob("*.pdf")

for file in pdflist:
	if "apple" or "banana" or "orange" or "Orange" or "pear" or "cherry" or "Cherry" or "Apple" or "Banana" or "Pear" in str(file):
		def convert(fname, pages=None):
			if not pages:
				pagenums = set()
				pagenums = set(pages)

			output = StringIO()
			manager = PDFResourceManager()
			converter = TextConverter(manager, output, laparams=LAParams())
			interpreter = PDFPageInterpreter(manager, converter)

			infile = open(fname, 'rb')
			for page in PDFPage.get_pages(infile, pagenums):
			text = output.getvalue()
			return text
		def convertMultiple(pdfDir, txtDir):
			if pdfDir == "": pdfDir = os.getcwd() + "\\" #if no pdfDir passed in 
			for pdf in os.listdir(pdfDir): #iterate through pdfs in pdf directory
				fileExtension = pdf.split(".")[-1]
				if fileExtension == "pdf":
					pdfFilename = pdfDir + pdf 
					text = convert(pdfFilename) #get string of text content of pdf
					text1line = str(text).replace("\n", " ")
					if "apple" or "Apple" in str(file):
						appledate ='enquiry dated (.*), we can confirm', text)
						applecompanysource =' shares of (.*) registered in', text)
						applecompany = str(applecompanysource).split('PLC')[0] + " PLC"
						appledesignation ='registered in the name of (.*)Voting', text1line)
						f.write(str(statestreetdate) + ",")
					elif "banana" or "Banana" in str(file):
						bananadate ='- As at (.*)', text)
						f.write(str(bananadate) + ",")
					elif "cherry" or "Cherry" in str(file):
						cherrydate ='DATE : (.*)', text)
						f.write(str(cherrydate) + ",")



		pdfDir = "C:/Users/thisisme/Desktop/DataAquisition/"
		txtDir = "C:/Users/thisisme/Desktop/DataAquisition/"
		convertMultiple(pdfDir, txtDir)


Your problem is in line 12 (or at least, that's the first error I see). You need to have an expression on each side of the or (see here for details)
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

(Mar-05-2019, 03:13 PM)CaptainCsaba Wrote: if "apple" or "banana" or "orange" or "Orange" or "pear" or "cherry" or "Cherry" or "Apple" or "Banana" or "Pear" in str(file):

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  python gives wrong string length and wrong character thienson30 2 183 Oct-15-2019, 08:54 PM
Last Post: Gribouillis

Forum Jump:

Users browsing this thread: 1 Guest(s)