欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于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ā)貨單。

過程:

先寫一個(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)文章

  • 使用Python第三方庫發(fā)送電子郵件的示例代碼

    使用Python第三方庫發(fā)送電子郵件的示例代碼

    本文主要介紹了使用Python第三方庫發(fā)送電子郵件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • pyqt6實(shí)現(xiàn)QTimer定時(shí)器介紹和使用場景

    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
  • 基于Python實(shí)現(xiàn)敲電子木魚效果

    基于Python實(shí)現(xiàn)敲電子木魚效果

    這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)敲電子木魚加功德效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2024-12-12
  • Python中的choice()方法使用詳解

    Python中的choice()方法使用詳解

    這篇文章主要介紹了Python中的choice()方法使用詳解,是Python入門中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • 解決pycharm不能自動(dòng)補(bǔ)全第三方庫的函數(shù)和屬性問題

    解決pycharm不能自動(dòng)補(bǔ)全第三方庫的函數(shù)和屬性問題

    這篇文章主要介紹了解決pycharm不能自動(dòng)補(bǔ)全第三方庫的函數(shù)和屬性問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python監(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í)例

    今天小編就為大家分享一篇python監(jiān)控進(jìn)程狀態(tài),記錄重啟時(shí)間及進(jìn)程號(hào)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python使用正則表達(dá)式匹配反斜杠\遇到的問題

    python使用正則表達(dá)式匹配反斜杠\遇到的問題

    在學(xué)習(xí)Python正則式的過程中,有一個(gè)問題一直困擾我,如何去匹配一個(gè)反斜杠(即“\”),下面這篇文章主要給大家介紹了關(guān)于python使用正則表達(dá)式匹配反斜杠\的相關(guān)資料,需要的朋友可以參考下
    2022-09-09
  • 使用 Python 查找本月的最后一天的方法匯總

    使用 Python 查找本月的最后一天的方法匯總

    這篇文章主要介紹了使用 Python 查找本月的最后一天,在本文中,我們學(xué)習(xí)了使用 datetime 和 calendar 等內(nèi)置庫以及 arrow 和 pandas 等第三方庫在 Python 中查找月份最后一天的各種方法,需要的朋友可以參考下
    2023-05-05
  • 詳解Python locals()的陷阱

    詳解Python locals()的陷阱

    這篇文章主要介紹了詳解Python locals()的陷阱,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-03-03
  • Python requests模塊用法詳解

    Python requests模塊用法詳解

    這篇文章主要介紹了Python requests模塊用法,Python內(nèi)置了requests模塊,該模塊主要用來發(fā)送HTTP請(qǐng)求,requests模塊比urllib模塊更簡潔
    2023-02-02

最新評(píng)論