网站地图 wordpress,做网站排名推广效果怎么样,百度电脑端入口,平面设计班起因#xff0c; 目的:
某个小店#xff0c;想做个发货单。
过程:
先写一个 html 模板。准备数据#xff0c; 一般是从数据库读取#xff0c;也可以是 json 格式#xff0c;或是 python 字典。总之#xff0c;是数据内容。使用 jinja2 来渲染模板。最终的结果可以是 h…起因 目的:
某个小店想做个发货单。
过程:
先写一个 html 模板。准备数据 一般是从数据库读取也可以是 json 格式或是 python 字典。总之是数据内容。使用 jinja2 来渲染模板。最终的结果可以是 html, 也可以是 pdf, 反正可以直接让打印机打印。
代码 1 html 模板
!DOCTYPE html
html
headtitleInvoice/titlestylebody {font-family: Arial, sans-serif;margin: 0;padding: 0;}.container {max-width: 800px;margin: 0 auto;padding: 20px;border: 1px solid #ccc;}table {width: 100%;border-collapse: collapse;}h2 {margin-bottom: 50px;text-align: center;}th, td {padding: 10px;text-align: left;border-bottom: 1px solid #ccc;}.invoice-details {margin-bottom: 20px;text-align: left;}.invoice-details p {margin: 0;font-size: 16px;}.invoice-details strong {font-weight: bold;}.invoice-items {margin-top: 60px;margin-bottom: 20px;}.invoice-items th {font-weight: bold;text-align: left;}.invoice-items td {text-align: left;}.invoice-total {text-align: right;margin-right: 30px;}.invoice-total strong {font-size: 20px;font-weight: bold;}.bottom {margin-top: 40px;text-align: right;}.info {margin-top: 60px;}.signature {width: 200px;height: auto;}media print {.container {border: none;}}/style
/head
bodydiv classcontainerh2{{ invoice_header }}/h2div classinvoice-detailspstrongInvoice number:/strong {{ invoice_number }}/ppstrongDate:/strong {{ payment_received_date }}/ppstrongBilled to:/strong {{ billed_to }}/ppstrongAddress: /strong {{ billed_to_address }}/p/divdiv classinvoice-itemstabletheadtrthDescription/ththAmount/ththCurrency/th/tr/theadtbodytrtd{{ work_description }}/tdtd{{ amount }}/tdtd{{ currency }}/td/tr/tbody/table/divp classinfoPaid in {{ currency }} to {{ person }},IBAN strong{{account_iban}}/strong,SWIFT strong{{ swift }}/strong/pdiv classinvoice-details bottompstrong{{ person }}/strong/ppstrongEmail:/strong {{ email }}/p/ppstrongPhone:/strong {{ phone }}/p/div/div
/body
/html代码 2 python
# file: render_html.py
# 把 json 数据写入到 html 里面进行渲染输出实际数据的 html
from jinja2 import FileSystemLoader, Environment# 参考来源
# https://dboostme.medium.com/how-to-generate-invoices-using-python-playwright-d77839af4b1e# 实际上下面这个教程写的很不错
# https://practicalpython.yasoob.me/chapter3invoice_number 1context {invoice_number: invoice_number,invoice_header: Invoice for Delivering Pizza,amount: 10,currency: USD,account: account_id,account_iban: account_iban,swift: account_swift,work_description: Delivering pizza on motorbike,person: James Bond,email: jamesbond.mi6,phone: 4476898123428,billed_to: MI-6,billed_to_address: 85 Albert Embankment,payment_received_date: 2023-08-05
}# 整体的逻辑是 找个模板文件用 jinja2 渲染数据输出 html 文件template_loader FileSystemLoader(./)
template_env Environment(loadertemplate_loader)template template_env.get_template(invoice_sample.html) # html 模板文件invoice_html template.render(context)file_name foutput_{invoice_number}.html
with open(file_name, w) as html_file:html_file.write(invoice_html)最终的效果 其实也可以加一个 logo 加个图片更好看一些。比如像这个样:
代码 3, 把 html 转为 pdf
import pdfkit # 这个是可行的
# 这种方式按照运行失败
# from weasyprint import HTML # pip install weasyprint
config pdfkit.configuration(wkhtmltopdfrC:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe)
# ret pdfkit.from_file(1.html, 1.pdf, configurationconfig)
ret pdfkit.from_file(output_1_new.html, output_1_new.pdf, configurationconfig)
print(ret)
# True结论 todo
整体比较简单但是对有些人或许很有用。 老哥留步支持一下。
文章转载自: http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.dzzjq.cn.gov.cn.dzzjq.cn http://www.morning.wqwbj.cn.gov.cn.wqwbj.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.htjwz.cn.gov.cn.htjwz.cn http://www.morning.xqkcs.cn.gov.cn.xqkcs.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.fbdtd.cn.gov.cn.fbdtd.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.dnmgr.cn.gov.cn.dnmgr.cn http://www.morning.smggx.cn.gov.cn.smggx.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.rbcw.cn.gov.cn.rbcw.cn http://www.morning.fnywn.cn.gov.cn.fnywn.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.lwzgn.cn.gov.cn.lwzgn.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn http://www.morning.mpbgy.cn.gov.cn.mpbgy.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.nbnq.cn.gov.cn.nbnq.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn http://www.morning.gbkkt.cn.gov.cn.gbkkt.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.kaylyea.com.gov.cn.kaylyea.com http://www.morning.lmfmd.cn.gov.cn.lmfmd.cn http://www.morning.fthcn.cn.gov.cn.fthcn.cn http://www.morning.ysjjr.cn.gov.cn.ysjjr.cn http://www.morning.kjlia.com.gov.cn.kjlia.com http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.kdbbm.cn.gov.cn.kdbbm.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.nzklw.cn.gov.cn.nzklw.cn http://www.morning.rymb.cn.gov.cn.rymb.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.tjpmf.cn.gov.cn.tjpmf.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.tfpmf.cn.gov.cn.tfpmf.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.nnykz.cn.gov.cn.nnykz.cn http://www.morning.fsqbx.cn.gov.cn.fsqbx.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.jcffp.cn.gov.cn.jcffp.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.fnywn.cn.gov.cn.fnywn.cn http://www.morning.lnbyk.cn.gov.cn.lnbyk.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.qwpyf.cn.gov.cn.qwpyf.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.cbqqz.cn.gov.cn.cbqqz.cn http://www.morning.hqbnx.cn.gov.cn.hqbnx.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.qxmpp.cn.gov.cn.qxmpp.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.pghfy.cn.gov.cn.pghfy.cn http://www.morning.njntp.cn.gov.cn.njntp.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.wjxyg.cn.gov.cn.wjxyg.cn http://www.morning.cjrmf.cn.gov.cn.cjrmf.cn