序言
上一篇咱们实现了多个表格数据合并到一个表格,本次咱们来学习如何将表格数据分开导出为PDF文件。
部分数据
然后需要安装一下这个软件 wkhtmltopdf
不知道怎么下载的可以在电脑端左侧扫一下找到我要
效果展示
数据单独导出为一个PDF
实现代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import pdfkit import openpyxl import os target_dir = '经销商预算' 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年广东经销商预算目标</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> """ 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经销商目标.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实现将Excel内容批量导出为PDF文件的文章就介绍到这了,更多相关Python Excel导出为PDF内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/fei347795790/article/details/124196275