這篇文章給大家分享的是有關Python如何將excel內容批量轉化為pdf的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創新互聯公司-專業網站定制、快速模板網站建設、高性價比潢川網站開發、企業建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式潢川網站制作公司更省心,省錢,快速模板網站建設找我們,業務覆蓋潢川地區。費用合理售后完善,十余年實體公司更值得信賴。
項目名稱:
將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 class="blod"> <td>code</td> <td>name</td> <td>number</td> <td>money</td> <td>total</td> <td></td> </tr> </table> </body> </html>
最后運行代碼,效果如下圖
感謝各位的閱讀!關于“Python如何將excel內容批量轉化為pdf”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
文章標題:Python如何將excel內容批量轉化為pdf
本文URL:http://m.kartarina.com/article30/geccpo.html
成都網站建設公司_創新互聯,為您提供網站設計公司、靜態網站、做網站、網站導航、外貿網站建設、定制開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯