旧代码不能与新版本的PDFMin一起使用

2024-04-29 00:07:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我从前学院“继承”了一个程序。这个程序以前运行得很好,但对我不起作用。原因是学院使用了旧版本的PDFMiner(我不知道是哪个版本),而我有一个新版本。代码如下:

filpath=r"...."
rsrcmgr = PDFResourceManager()
retstr = StringIO()
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
fp = open(filepath,'rb')
interpreter = PDFPageInterpreter(rsrcmgr,device)
parser = PDFParser(fp)
doc = PDFDocument()
parser.set_document(doc)
doc.set_parser(parser)
doc.initialize('') 

一开始我有错误

^{pr2}$

我将doc=PDFDocument()行改为doc=PDFDocument(解析器)。这是有效的,但现在我有了错误

'PDFDocument' object has no attribute 'set_parser'

显然是从生产线来的doc.set_解析器(解析器)。 我现在该怎么办?在

附加信息:旧程序中的导入行是

from pdfminer.pdfparser import PDFParser, PDFDocument

现在不行了,我不得不改成两行

from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument

提前谢谢!在

更新。 最后,我以一种“廉价”的方式解决了我的问题——我安装了旧版本的pdfminer,所以我不需要修改代码中的任何内容。在


Tags: 代码fromimport程序版本parser解析器doc
1条回答
网友
1楼 · 发布于 2024-04-29 00:07:05

你看到docs了吗?在

from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfpage import PDFTextExtractionNotAllowed
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice

# Open a PDF file.
fp = open('mypdf.pdf', 'rb')
# Create a PDF parser object associated with the file object.
parser = PDFParser(fp)
# Create a PDF document object that stores the document structure.
# Supply the password for initialization.
document = PDFDocument(parser, password)
# Check if the document allows text extraction. If not, abort.
if not document.is_extractable:
    raise PDFTextExtractionNotAllowed
# Create a PDF resource manager object that stores shared resources.
rsrcmgr = PDFResourceManager()
# Create a PDF device object.
device = PDFDevice(rsrcmgr)
# Create a PDF interpreter object.
interpreter = PDFPageInterpreter(rsrcmgr, device)
# Process each page contained in the document.
for page in PDFPage.create_pages(document):
    interpreter.process_page(page)

从链接中逐字复制。在

相关问题 更多 >