基于python實現(xiàn)制作發(fā)貨單
起因, 目的:
某個小店,想做個發(fā)貨單。
過程:
先寫一個 html 模板。
準備數(shù)據(jù), 一般是從數(shù)據(jù)庫讀取,也可以是 json 格式,或是 python 字典??傊?,是數(shù)據(jù)內(nèi)容。
使用 jinja2 來渲染模板。
最終的結(jié)果可以是 html, 也可以是 pdf, 反正可以直接讓打印機打印。
代碼 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 里面,進行渲染,輸出實際數(shù)據(jù)的 html
from jinja2 import FileSystemLoader, Environment
# 參考來源
# https://dboostme.medium.com/how-to-generate-invoices-using-python-playwright-d77839af4b1e
# 實際上,下面這個教程寫的很不錯!
# 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"
}
# 整體的邏輯是: 找個模板文件,用 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)
最終的效果

其實也可以加一個 logo, 加個圖片,更好看一些。比如像這個樣:

代碼 3, 把 html 轉(zhuǎn)為 pdf
import pdfkit # 這個是可行的?。?!
"""
# 這種方式按照運行失敗?。?
# 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
整體比較簡單,但是對有些人或許很有用。
到此這篇關(guān)于基于python實現(xiàn)制作發(fā)貨單的文章就介紹到這了,更多相關(guān)python制作發(fā)貨單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決pycharm不能自動補全第三方庫的函數(shù)和屬性問題
這篇文章主要介紹了解決pycharm不能自動補全第三方庫的函數(shù)和屬性問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python監(jiān)控進程狀態(tài),記錄重啟時間及進程號的實例
今天小編就為大家分享一篇python監(jiān)控進程狀態(tài),記錄重啟時間及進程號的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07

