Dominate 是个 Python 库,使用 DOM API 来创建和操作 HTML 文档。允许用户用很简洁的纯 Python 来编写 HTML 页面。
例子:
doc = dominate.document(title='Test Report HTML')
def creat_html(result_pic_list,devices,report_dir):
with doc.head:
link(rel='stylesheet', href='js/image_click.css')
script(type='text/javascript', src='js/jquery.min.js')
script(type='text/javascript', src='js/jquery.imgbox.pack.js')
script(type='text/javascript', src='js/jquery.click.pic.js')
with doc.add(div(id='content')):
with table(border='1').add(tbody()):
with l1:
td(colspan="4").add(chip_name)
report_dir = os.path.join(report_dir,'report.html')
html转pdf
1,首先需要安装pdfkit库,以及wkhtmltopdf库
2,下载wkhtmltopdf程序,将bin目录添加到path中,然后通过以下代码可以实现html转换pdf
def html_to_pdf(input_path,out_path):
options = {
"enable-local-file-access": None
}
root_path = os.path.abspath(os.path.join(os.getcwd(), ".."))
wkhtmltopdf = os.path.join(root_path,'tools','wkhtmltox','bin','wkhtmltopdf.exe')
# config = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltox\bin\wkhtmltopdf.exe')
log('wkhtmltopdf path is :' + wkhtmltopdf)
config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf)
try:
pdfkit.from_file(input_path, out_path, configuration=config, options=options)
except Exception as e:
my_log.log(e)