前言
自動化辦公:
走上雜務作業的快車道,當你實作一部分的作業自動化以后,你就會走上一個“作業自由”的快車道,因為你有更多的時間能用來研究如何去更高效的完成余下的那堆作業,然后,每天作業的時間就會越來越短,
專案名稱:
將excel內容批量轉化為pdf
知識點:
- python 操作 excel
- python 操作 pdf
- html
開發環境:
- 解釋器: Python 3.6.5 | Anaconda, Inc.
- 編輯器: pycharm 社區版
代碼
匯入工具
import openpyxl import pdfkit
加載一個本地檔案
workbook = openpyxl.load_workbook('2020經銷商目標.xlsx') def func(money): # print(money) str_number = f'{money}' str_list = list(str_number[::-1]) str_ = "" i = 1 for no, char in enumerate(str_list): str_ += char # print(no, char) if i % 3 == 0 and len(str_list) != i: str_ += ',' i += 1 # print(str_[::-1]) return str_[::-1] sheet1 = workbook['Sheet1'] print(sheet1) for row in list(sheet1.rows)[3:]: for col in row[1:]: print(col.value, end='\t') code = row[1].value name = row[2].value number = row[3].value money = row[4].value total = row[5].value print(code, name, number, money) str_number = func(number) str_money = func(money) str_total = func(total) html = html.replace('code', code) html = html.replace('name', name) html = html.replace('number', f'{str_number}.00') html = html.replace('money', f'{str_money:}.00') html = html.replace('total', f'{str_total}.00') # print(html) with open('example.html', mode='w', encoding='utf-8') as f: f.write(html)
配置pdf的轉化軟體
config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
from_file 檔案的名字 保存檔案的名字 轉化軟體的配置
pdfkit.from_file('example.html', f'{name}.pdf', configuration=config)
轉化為PDF
html = """ <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> table { font-size: 22px; width: 850px; } .blod { font-weight: bolder; } </style> </head> <body> <table border="1" align="center"> <tr> <td align="center" colspan="6">2020年廣東經銷商預算目標</td> </tr> <tr> <td>經銷商代碼</td> <td>經銷商名稱</td> <td>成車數量</td> <td>成車金額</td> <td>商品金額</td> <td>客戶簽字</td> </tr> <tr > <td>code</td> <td>name</td> <td>number</td> <td>money</td> <td>total</td> <td></td> </tr> </table> </body> </html>
最后運行代碼,效果如下圖
PS:如有需要Python學習資料的小伙伴可以加下方的群去找免費管理員領取
可以免費領取原始碼、專案實戰視頻、PDF檔案等
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/51298.html
標籤:Python
