pdf如何用python读取?

2025-12-02 0 15,579

python中可以使用pdfminer库来读取PDF文件中的内容。

安装命令:

pip install pdfminer
pip install pdfminer3k

python中读取PDF文件代码:

from urllib.request import urlopen
from pdfminer.pdfinterp import PDFResourceManager, process_pdf
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from io import StringIO
from io import open

def readPDF(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()
    return content

pdfFile = urlopen("http://pythonscraping.com/pages/warandpeace/chapter1.pdf")
outputString = readPDF(pdfFile)
print(outputString)
pdfFile.close()

解析pdf文件用到的类:

PDFParser:从一个文件中获取数据

PDFDocument:保存获取的数据,和PDFParser是相互关联的

PDFPageInterpreter处理页面内容

PDFDevice将其翻译成你需要的格式

PDFResourceManager用于存储共享资源,如字体或图像。

更多Python知识请关注Python自学网

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 常见问题 pdf如何用python读取? https://www.zhiup.top/6832.html

相关