我正在尝试使用HTML Converter将PDF文件转换为HTML格式。下面提供的是我正在使用的代码。
from django.conf import settings
settings.configure(PDF_MINER_IS_STRICT = True)
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import HTMLConverter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
import re
import csv
def convert_pdf_to_html(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = HTMLConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = file(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0 #is for all
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)
fp.close()
device.close()
str = retstr.getvalue().encode('utf-8')
retstr.close()
return str.encode('utf-8')
在运行时,我收到以下错误:
Traceback (most recent call last):
File "convertPDFToHTML.py", line 32, in <module>
print convert_pdf_to_html(path)
File "convertPDFToHTML.py", line 18, in convert_pdf_to_html
device = HTMLConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
File "C:\Python27\lib\site-packages\pdfminer\converter.py", line 247, in __init__
PDFConverter.__init__(self, rsrcmgr, outfp, codec=codec, pageno=pageno, laparams=laparams)
File "C:\Python27\lib\site-packages\pdfminer\converter.py", line 167, in __init__
self.outfp.write(u"é")
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
我不知道如何让HTMLConverter的'converter.py'遵循unicode编码。
请帮忙!
答案 0 :(得分:0)
我设法通过使用.decode('utf-8')
代替.encode('utf-8')
from django.conf import settings
settings.configure(PDF_MINER_IS_STRICT = True)
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import HTMLConverter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from cStringIO import StringIO
import re
import csv
def convert_pdf_to_html(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = HTMLConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = file(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0 #is for all
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)
fp.close()
device.close()
str = retstr.getvalue().decode('utf-8')
retstr.close()
return str # no need to encode