基于python實(shí)現(xiàn)制作發(fā)貨單
起因, 目的:
某個(gè)小店,想做個(gè)發(fā)貨單。
過程:
先寫一個(gè) html 模板。
準(zhǔn)備數(shù)據(jù), 一般是從數(shù)據(jù)庫讀取,也可以是 json 格式,或是 python 字典。總之,是數(shù)據(jù)內(nèi)容。
使用 jinja2 來渲染模板。
最終的結(jié)果可以是 html, 也可以是 pdf, 反正可以直接讓打印機(jī)打印。
代碼 1, html 模板
<!DOCTYPE html> <html> <head> <title>Invoice</title> <style> body { 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> <body> <div class="container"> <h2>{{ invoice_header }}</h2> <div class="invoice-details"> <p><strong>Invoice number:</strong> {{ invoice_number }}</p> <p><strong>Date:</strong> {{ payment_received_date }}</p> <p><strong>Billed to:</strong> {{ billed_to }}</p> <p><strong>Address: </strong> {{ billed_to_address }}</p> </div> <div class="invoice-items"> <table> <thead> <tr> <th>Description</th> <th>Amount</th> <th>Currency</th> </tr> </thead> <tbody> <tr> <td>{{ work_description }}</td> <td>{{ amount }}</td> <td>{{ currency }}</td> </tr> </tbody> </table> </div> <p class="info"> Paid in {{ currency }} to {{ person }}, IBAN <strong>{{account_iban}}</strong>, SWIFT <strong>{{ swift }}</strong> </p> <div class="invoice-details bottom"> <p><strong>{{ person }}</strong></p> <p><strong>Email:</strong> {{ email }}</p></p> <p><strong>Phone:</strong> {{ phone }}</p> </div> </div> </body> </html>
效果圖
代碼 2, python
# file: render_html.py # 把 json 數(shù)據(jù)寫入到 html 里面,進(jìn)行渲染,輸出實(shí)際數(shù)據(jù)的 html from jinja2 import FileSystemLoader, Environment # 參考來源 # https://dboostme.medium.com/how-to-generate-invoices-using-python-playwright-d77839af4b1e # 實(shí)際上,下面這個(gè)教程寫的很不錯(cuò)! # https://practicalpython.yasoob.me/chapter3 invoice_number = 1 context = { "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": "james@bond.mi6", "phone": "+4476898123428", "billed_to": "MI-6", "billed_to_address": "85 Albert Embankment", "payment_received_date": "2023-08-05" } # 整體的邏輯是: 找個(gè)模板文件,用 jinja2 渲染數(shù)據(jù),輸出 html 文件 template_loader = FileSystemLoader("./") template_env = Environment(loader=template_loader) template = template_env.get_template("invoice_sample.html") # html 模板文件 invoice_html = template.render(context) file_name = f"output_{invoice_number}.html" with open(file_name, "w") as html_file: html_file.write(invoice_html)
最終的效果
其實(shí)也可以加一個(gè) logo, 加個(gè)圖片,更好看一些。比如像這個(gè)樣:
代碼 3, 把 html 轉(zhuǎn)為 pdf
import pdfkit # 這個(gè)是可行的!?。? """ # 這種方式按照運(yùn)行失?。。? # from weasyprint import HTML # pip install weasyprint """ config = pdfkit.configuration(wkhtmltopdf=r"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe") # ret = pdfkit.from_file('1.html', '1.pdf', configuration=config) ret = pdfkit.from_file('output_1_new.html', 'output_1_new.pdf', configuration=config) print(ret) # True
整體比較簡單,但是對(duì)有些人或許很有用。
到此這篇關(guān)于基于python實(shí)現(xiàn)制作發(fā)貨單的文章就介紹到這了,更多相關(guān)python制作發(fā)貨單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pyqt6實(shí)現(xiàn)QTimer定時(shí)器介紹和使用場景
PyQt6中的QTimer是一個(gè)定時(shí)器類,用于在指定的時(shí)間間隔內(nèi)執(zhí)行某個(gè)操作,本文主要介紹了pyqt6實(shí)現(xiàn)QTimer定時(shí)器介紹和使用場景,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02解決pycharm不能自動(dòng)補(bǔ)全第三方庫的函數(shù)和屬性問題
這篇文章主要介紹了解決pycharm不能自動(dòng)補(bǔ)全第三方庫的函數(shù)和屬性問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03python監(jiān)控進(jìn)程狀態(tài),記錄重啟時(shí)間及進(jìn)程號(hào)的實(shí)例
今天小編就為大家分享一篇python監(jiān)控進(jìn)程狀態(tài),記錄重啟時(shí)間及進(jìn)程號(hào)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07