亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于python實(shí)現(xiàn)制作發(fā)貨單

 更新時(shí)間:2024年11月01日 11:26:00   作者:waterHBO  
這篇文章主要為大家詳細(xì)介紹了如何基于python實(shí)現(xiàn)制作發(fā)貨單,并將還html轉(zhuǎn)為pdf,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

起因, 目的:

某個(gè)小店,想做個(gè)發(fā)貨單。

過(guò)程:

先寫(xiě)一個(gè) html 模板。

準(zhǔn)備數(shù)據(jù), 一般是從數(shù)據(jù)庫(kù)讀取,也可以是 json 格式,或是 python 字典??傊菙?shù)據(jù)內(nèi)容。

使用 jinja2 來(lái)渲染模板。

最終的結(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ù)寫(xiě)入到 html 里面,進(jìn)行渲染,輸出實(shí)際數(shù)據(jù)的 html
from jinja2 import FileSystemLoader, Environment

# 參考來(lái)源
# https://dboostme.medium.com/how-to-generate-invoices-using-python-playwright-d77839af4b1e

# 實(shí)際上,下面這個(gè)教程寫(xiě)的很不錯(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

整體比較簡(jiǎn)單,但是對(duì)有些人或許很有用。

到此這篇關(guān)于基于python實(shí)現(xiàn)制作發(fā)貨單的文章就介紹到這了,更多相關(guān)python制作發(fā)貨單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論