我試圖從比薩實用程序轉換html2pdf。請檢查下面的代碼。我得到了我無法弄清楚的錯誤。使用Python中的xhtml2pdf.pisa轉換阿拉伯文頁面
Traceback (most recent call last):
File "dewa.py", line 27, in <module>
html = html.encode(enc, 'replace')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd9 in position 203: ordinal not in range(128)
請檢查這裏的代碼。
from cStringIO import StringIO
from grab import Grab
from grab.tools.lxml_tools import drop_node, render_html
from grab.tools.text import remove_bom
from lxml import etree
import grab.error
import inspect
import lxml
import os
import sys
import xhtml2pdf.pisa as pisa
enc = 'utf-8'
filePath = '~/Desktop/dewa'
##############################
g = Grab()
g.go('http://www.dewa.gov.ae/arabic/aboutus/dewahistory.aspx')
html = g.response.body
html = html.replace('bgcolor="EDF389"', 'bgcolor="#EDF389"')
''' clear page '''
html = html.encode(enc, 'replace')
print html
f = file(filePath + '.html' , 'wb')
f.write(html)
f.flush()
f.close()
''' Save PDF '''
pdfresult = StringIO()
pdf = pisa.pisaDocument(StringIO(html), pdfresult, encoding = enc)
f = file(filePath + '.pdf', 'wb')
f.write(pdfresult.getvalue())
f.flush()
f.close()
pdfresult.close()
谷歌搜索的**「ASCII」編解碼器**堆棧溢出不能解碼字節返回12K +結果。你可能想從那開始...... – dda