我目前正在尝试从整个PDF中提取文本。我尝试从 PDF 的单页中提取文本并且它可以正常工作,但是当我尝试提取整个 PDF 时,它给了我这个错误:
Traceback (most recent call last):
File "D:/PDF_extract_1/main.py", line 35, in <module>
extract_whole_pdf()
File "D:/PDF_extract_1/main.py", line 26, in extract_whole_pdf
final = final + "\n" + data
TypeError: can only concatenate str (not "NoneType") to str
作为参考,这是我从单页提取时使用的代码:
def extract_first():
pdf = pdfplumber.open("pdftest2.pdf")
page = pdf.pages[6] #just for example, I chose page 5 of the PDF
text = page.extract_text()
print("First page data : {}".format(text))
with open("pdf_pages.txt", "w", encoding='utf-8') as f:
f.write(text)
pdf.close()
这是我用来提取整个 PDF 的代码:
def extract_whole_pdf():
pdf = pdfplumber.open("pdftest2.pdf")
n = len(pdf.pages)
final = ""
for page in range(n):
data = pdf.pages[page].extract_text()
final = final + "\n" + data
print("Whole document data : {}".format(final))
with open("pdf_extract.txt", "w", encoding='utf-8') as f:
f.write(final)
pdf.close()
我注意到这个问题被问了很多,但它们似乎不适用于我的问题。其中一个问题有类似的错误,但情况与我的不同。