最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于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)文章

  • 詳解anaconda離線安裝pytorchGPU版

    詳解anaconda離線安裝pytorchGPU版

    這篇文章主要介紹了詳解anaconda離線安裝pytorchGPU版,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python3格式化字符串 f-string的高級(jí)用法(推薦)

    python3格式化字符串 f-string的高級(jí)用法(推薦)

    從Python 3.6開(kāi)始,f-string是格式化字符串的一種很好的新方法。與其他格式化方式相比,它們不僅更易讀,更簡(jiǎn)潔,不易出錯(cuò),而且速度更快!本文重點(diǎn)給大家介紹python3格式化字符串 f-string的高級(jí)用法,一起看看吧
    2020-03-03
  • jupyter代碼塊沒(méi)有運(yùn)行圖標(biāo)的解決方案

    jupyter代碼塊沒(méi)有運(yùn)行圖標(biāo)的解決方案

    這篇文章主要介紹了jupyter代碼塊沒(méi)有運(yùn)行圖標(biāo)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • python進(jìn)階教程之模塊(module)介紹

    python進(jìn)階教程之模塊(module)介紹

    這篇文章主要介紹了python進(jìn)階教程之模塊(module)介紹,本文講解了基礎(chǔ)知識(shí)、引用方法、搜索的路徑、模塊包等知識(shí),需要的朋友可以參考下
    2014-08-08
  • python格式化輸出保留2位小數(shù)的實(shí)現(xiàn)方法

    python格式化輸出保留2位小數(shù)的實(shí)現(xiàn)方法

    這篇文章主要介紹了python格式化輸出保留2位小數(shù)的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2019-07-07
  • Python下載的11種姿勢(shì)(小結(jié))

    Python下載的11種姿勢(shì)(小結(jié))

    這篇文章主要介紹了Python下載的11種姿勢(shì)(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 基于Python編寫(xiě)監(jiān)控視頻存儲(chǔ)計(jì)算器

    基于Python編寫(xiě)監(jiān)控視頻存儲(chǔ)計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了如何基于Python編寫(xiě)一個(gè)監(jiān)控視頻存儲(chǔ)計(jì)算器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • python的django寫(xiě)頁(yè)面上傳文件及遇到的問(wèn)題小結(jié)

    python的django寫(xiě)頁(yè)面上傳文件及遇到的問(wèn)題小結(jié)

    這篇文章主要介紹了python的django寫(xiě)頁(yè)面上傳文件以及遇到的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • python爬取豆瓣評(píng)論制作詞云代碼

    python爬取豆瓣評(píng)論制作詞云代碼

    大家好,本篇文章主要講的是python爬取豆瓣評(píng)論制作詞云代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Python代碼解決RenderView窗口not found問(wèn)題

    Python代碼解決RenderView窗口not found問(wèn)題

    這篇文章主要介紹了Python代碼解決RenderView窗口not found問(wèn)題,需要的朋友可以參考下
    2016-08-08

最新評(píng)論

晴隆县| 微博| 雅安市| 桦南县| 荥阳市| 郁南县| 泾阳县| 南澳县| 南充市| 雷州市| 兴化市| 宁武县| 玛曲县| 潞西市| 岢岚县| 昌江| 正宁县| 泰兴市| 尼勒克县| 安康市| 梁平县| 楚雄市| 丰都县| 平和县| 宁蒗| 墨竹工卡县| 庆城县| 富阳市| 都兰县| 葫芦岛市| 正阳县| 成都市| 资源县| 治县。| 北宁市| 武乡县| 北川| 寻乌县| 灵宝市| 雷山县| 射阳县|