相去甚远
我已经让pypdf可以很好地处理单个pdf文件,但我似乎无法让它处理一小部分文件,或者在for循环中处理多个pdf,而不会因为字符串不可调用而失败。有什么想法可以作为变通办法吗?
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:GISMasterPermits12300983.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 SettingsdknightDesktopreadpdf.py", line 50, in <module>
print getPDFContent(d).encode("ascii", "ignore")
File "C:Documents and SettingsdknightDesktopreadpdf.py", line 32, in getPDFContent
pdf = pyPdf.PdfFileReader(file(path, "rb"))
TypeError: 'str' object is not callable
我使用了一个列表,但我得到了完全相同的错误,我不认为这会有什么大不了的,但现在它正在变成一个。我知道我可以在arcpy中解决类似的问题,但这与
尽量不要对变量名使用内置类型:
不要这样做:
for file in files:
改为:
for myfile in files: