-2

有没有可以工作的代码片段?我已经尝试过将pdf转换为html

from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import os
import contextlib 
import tempfile
rsrcmgr = PDFResourceManager()
laparams = LAParams()
converter = HTMLConverter if format == 'html' else TextConverter
out_file = "A:\folder"
in_file = "A:\folder\pyhtml.html"
pdf_filename = 'insurance.pdf'
device = converter(rsrcmgr, out_file, codec='utf-8', laparams=laparams)
PDFPage.get_pages(rsrcmgr, device, in_file, pagenos=[1], maxpages=1)

with contextlib.closing(tempfile.NamedTemporaryFile(mode='r', suffix='.xml')) as xmlin:
    cmd = 'pdftohtml -xml -nodrm -zoom 1.5 -enc UTF-8 -noframes "%s" "%s"' % (
            pdf_filename, xmlin.name.rpartition('.')[0])
    os.system(cmd + " >/dev/null 2>&1")
    result = xmlin.read().decode('utf-8')

当我运行上面的代码时,它给了我以下错误

Traceback (most recent call last):
  File "a:\folder\new - Copy.py", line 14, in <module>
    device = converter(rsrcmgr, out_file, codec='utf-8', laparams=laparams)
AttributeError: 'str' object has no attribute 'write'
4

1 回答 1

1
AttributeError: 'str' object has no attribute 'write'

如果尝试.write这样做意味着您应该提供可写文件句柄而不是str,您可以使用with open... 它将为您关闭文件,如下所示,替换

in_file = "A:\folder\pyhtml.html"
device = converter(rsrcmgr, out_file, codec='utf-8', laparams=laparams)

使用

in_file = "A:\folder\pyhtml.html"
with open(in_file, "w") as out_file:
    device = converter(rsrcmgr, out_file, codec='utf-8', laparams=laparams)

如果您想了解有关open阅读内置函数文档的更多信息

于 2021-09-21T10:03:33.360 回答