![图片[1]-pdf如何用python读取?-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5ecf5f5fa8a5a983.jpg)
python中可以使用pdfminer库来读取PDF文件中的内容。
安装命令:
pipinstallpdfminer
pipinstallpdfminer3k
python中读取PDF文件代码:
fromurllib.requestimporturlopen
frompdfminer.pdfinterpimportPDFResourceManager,process_pdf
frompdfminer.converterimportTextConverter
frompdfminer.layoutimportLAParams
fromioimportStringIO
fromioimportopen
defreadPDF(pdfFile):
rsrcmgr=PDFResourceManager()
retstr=StringIO()
laparams=LAParams()
device=TextConverter(rsrcmgr,retstr,laparams=laparams)
process_pdf(rsrcmgr,device,pdfFile)
device.close()
content=retstr.getvalue()
retstr.close()
returncontent
pdfFile=urlopen("http://pythonscraping.com/pages/warandpeace/chapter1.pdf")
outputString=readPDF(pdfFile)
print(outputString)
pdfFile.close()
解析pdf文件用到的类:
原文来自:https://www.py.cnPDFParser:从一个文件中获取数据
PDFDocument:保存获取的数据,和PDFParser是相互关联的
PDFPageInterpreter处理页面内容
PDFDevice将其翻译成你需要的格式
PDFResourceManager用于存储共享资源,如字体或图像。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
















































暂无评论内容