0
我有一些PDF文檔,我無法使用PyPDF僅使用PDFMiner提取文本。以下代碼可以正常工作以從PDF中提取所有文本,它會遍歷整個文檔,然後返回所有文本。 有沒有辦法只能使用PDF的某些頁面? 我擁有的PDF格式都是2000-3000多長,我只需要每隔一頁就完成一次。使用PDFMiner處理單色頁面
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
def convert_pdf_to_txt(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec,laparams=laparams)
fp = 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)
text = retstr.getvalue()
fp.close()
device.close()
retstr.close()
return text
謝謝,這就是我一直在尋找。 – user2665140