在不使用 PyPdf2 的情况下,使用具有相同功能的 Pdfminer 库包,如下所示。我从中得到了代码,并且我想编辑它,这段代码给了我一个文本文件,其中单词之间有空格。我使用 anaconda 和 python 3.6。要为 python 3.6 安装 PdfMiner,您可以使用此链接。
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO
class PdfConverter:
def __init__(self, file_path):
self.file_path = file_path
# convert pdf file to a string which has space among words
def convert_pdf_to_txt(self):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8' # 'utf16','utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = open(self.file_path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos = set()
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password, caching=caching, check_extractable=True):
interpreter.process_page(page)
fp.close()
device.close()
str = retstr.getvalue()
retstr.close()
return str
# convert pdf file text to string and save as a text_pdf.txt file
def save_convert_pdf_to_txt(self):
content = self.convert_pdf_to_txt()
txt_pdf = open('text_pdf.txt', 'wb')
txt_pdf.write(content.encode('utf-8'))
txt_pdf.close()
if __name__ == '__main__':
pdfConverter = PdfConverter(file_path='sample.pdf')
print(pdfConverter.convert_pdf_to_txt())