Python實現(xiàn)將Excel內(nèi)容批量導出為PDF文件
序言
上一篇咱們實現(xiàn)了多個表格數(shù)據(jù)合并到一個表格,本次咱們來學習如何將表格數(shù)據(jù)分開導出為PDF文件。
部分數(shù)據(jù)

然后需要安裝一下這個軟件 wkhtmltopdf
不知道怎么下載的可以在電腦端左側掃一下找到我要
效果展示

數(shù)據(jù)單獨導出為一個PDF

實現(xiàn)代碼
import pdfkit
import openpyxl
import os
target_dir = '經(jīng)銷商預算'
if not os.path.exists(target_dir):
os.mkdir(target_dir)
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
table {
font-size: 22px;
font-weight: bolder;
width: 850px;
}
</style>
</head>
<body>
<table border="1" align="center" cellspacing="1">
<tr>
<td class='title' align="center" colspan="6">2020年廣東經(jīng)銷商預算目標</td>
</tr>
<tr>
<td>經(jīng)銷商代碼</td>
<td>經(jīng)銷商名稱</td>
<td>成車數(shù)量</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>
"""
def html_to_pdf(filename_html, filename_pdf):
"""HTML 2 PDF"""
config = pdfkit.configuration(wkhtmltopdf='D:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
pdfkit.from_file(filename_html, filename_pdf, configuration=config)
wb = openpyxl.load_workbook('2020經(jīng)銷商目標.xlsx')
sheet = wb['Sheet1']
print(sheet.rows)
for row in list(sheet.rows)[3:]:
data = [value.value for value in row]
data = data[1:-1]
format_html = html.replace('[code]', data[0])
format_html = format_html.replace('{name}', data[1])
format_html = format_html.replace('{number}', str(data[2]))
format_html = format_html.replace('{money}', f'{data[3]:.2f}')
format_html = format_html.replace('{total}', f'{data[4]:.2f}')
with open('example.html', mode='w', encoding='utf-8') as f:
f.write(format_html)
html_to_pdf('example.html', target_dir + os.path.sep + data[0] + " " + data[1] + '.pdf')
到此這篇關于Python實現(xiàn)將Excel內(nèi)容批量導出為PDF文件的文章就介紹到這了,更多相關Python Excel導出為PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PyTorch中flatten()?函數(shù)的用法實例小結
在PyTorch中,flatten函數(shù)的作用是將一個多維的張量轉(zhuǎn)換為一維的向量,它可以將任意形狀的張量轉(zhuǎn)換為一維,而不需要指定轉(zhuǎn)換后的大小,這篇文章主要介紹了PyTorch中flatten()?函數(shù)的用法,需要的朋友可以參考下2023-11-11
Pandas之排序函數(shù)sort_values()的實現(xiàn)
這篇文章主要介紹了Pandas之排序函數(shù)sort_values()的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07
python?Seaborn繪制統(tǒng)計圖全面指南(直方圖散點圖小提琴圖熱力圖相關系數(shù)圖多張合并)
這篇文章主要介紹了python?Seaborn繪制統(tǒng)計圖全面指南,包括直方圖,散點圖,小提琴圖,熱力圖,相關系數(shù)圖及多張圖合并的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2024-01-01
使用Python合并PDF文件并添加自定義目錄及頁腳的全過程
在處理文檔時,我們經(jīng)常遇到需要合并多個PDF文件并添加目錄及頁腳的情況,本文將介紹如何使用Python,特別是PyPDF2和reportlab庫來實現(xiàn)這一功能我們將通過一個實用的示例來演示整個過程,需要的朋友可以參考下2024-03-03
python GUI庫圖形界面開發(fā)之PyQt5窗口布局控件QStackedWidget詳細使用方法
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5窗口布局控件QStackedWidget詳細使用方法,需要的朋友可以參考下2020-02-02

