我已经让pypdf对单个pdf文件工作得很好,但我似乎无法让它适用于文件的lits,或者用于多个pdf的for循环,而不会失败,因为字符串不是调用。我可以将任何想法用作解决方法吗?
def getPDFContent(path):
content = ""
# Load PDF into pyPDF
pdf = pyPdf.PdfFileReader(file(path, "rb"))
# Iterate pages
for i in range(0, pdf.getNumPages()):
# Extract text from page and add to content
content += pdf.getPage(i).extractText() + "\n"
# Collapse whitespace
content = " ".join(content.replace(u"\xa0", " ").strip().split())
return content
#print getPDFContent(r"Z:\GIS\MasterPermits\12300983.pdf").encode("ascii", "ignore")
#find pdfs
for root, dirs, files in os.walk(folder1):
for file in files:
if file.endswith(('.pdf')):
d=os.path.join(root, file)
print getPDFContent(d).encode("ascii", "ignore")
Traceback (most recent call last):
File "C:\Documents and Settings\dknight\Desktop\readpdf.py", line 50, in <module>
print getPDFContent(d).encode("ascii", "ignore")
File "C:\Documents and Settings\dknight\Desktop\readpdf.py", line 32, in getPDFContent
pdf = pyPdf.PdfFileReader(file(path, "rb"))
TypeError: 'str' object is not callable
我正在使用一个列表,但我得到了完全相同的错误,我不认为这将是一个大问题,但现在它正在成为一个。我知道我能够在arcpy中解决类似的问题,但这没什么关系
答案 0 :(得分:2)
尽量不要为变量名使用内置类型:
不要这样做:
for file in files:
请改为:
for myfile in files: