Python实现将Excel表格内容批量分离导出为PDF文件,部分数据然后需要安装
Python实现将Excel表格内容批量分离导出为PDF文件,部分数据然后需要安装
上一篇咱们实现了多个表格数据合并到一个表格,本次咱们来学习如何将表格数据分开导出为PDF文件。
部分数据
然后需要安装一下这个软件 wkhtmltopdf
效果展示
将每个数据单独导出为一个PDF
代码实现
import pdfkit
import openpyxl
import os
# Python学习交流群 815624229
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表格,将多个表格内容合并到一个,
- 安装pyinstaller,打包python文件,以及遇到的问题,闪退
- python 一行命令开启网络间的文件共享,没有python环境
- Python3 反向按行读取大文件、日志read_reverse_bigfile,生成
- PyQt5 批量删除 Excel 重复数据,多个文件、自定义重复项
- Python 自动清理电脑垃圾文件,一键启动即可,垃圾清理
- 使用python爬取豆瓣电影短评评论内容,目标评论文本部
- python中_自动生成的_pycache__文件夹,在第一次执行代码
- 解决python opencv2在pycharm中没有代码提示的问题 (不修改
- Python 自动识别并批量转换文本文件编码,算是个偶尔能
评论关闭