【Python自动化办公】批量将Excel表格数据导出为PDF文件

前言

嗨喽~大家好呀,这里是魔王呐 !

Excel 格式在我们工作中经常需要用到的

不管是做数据统计还是做信息登记,Excel 都能发挥很强大的作用

也是目前最流行的个人计算机数据处理软件。

Excel 适合用于记录,但似乎并不太适合用于存档

于是有时候我们就需要将 Excel 格式的文档存储为PDF格式,这就方便对数据进行存档了。

Excel 文档转为 PDF 格式文档的方式又有很多种,比如直接用 Office 打开 Excel 文件,然后另存为 PDF 格式

【Python自动化办公】批量将Excel表格数据导出为PDF文件_第1张图片

这种将 Excel 文档转为 PDF 格式的方式,我相信大部分小伙伴都非常熟悉

但是,这种方式就能满足大家所有的场景和需求吗?

其实并不是的,因为假如我们有几千个甚至几万个 Excel 文档都需要转为 PDF 格式的文档

那你这样操作,岂不是要花很长很长的时间很多的精力?

那么今天,就来教大家一个轻松又省是得办法~python得自动化办公应用起来!!

【Python自动化办公】批量将Excel表格数据导出为PDF文件_第2张图片

部分数据

【Python自动化办公】批量将Excel表格数据导出为PDF文件_第3张图片

然后需要安装一下这个软件 wkhtmltopdf

不知道怎么下载的可以在电脑端左侧扫一下找到我要

效果展示
【Python自动化办公】批量将Excel表格数据导出为PDF文件_第4张图片【Python自动化办公】批量将Excel表格数据导出为PDF文件_第5张图片

代码实现

import pdfkit

import openpyxl
import os

target_dir = '经销商预算'

if not os.path.exists(target_dir):
    os.mkdir(target_dir)

html = """



    
    


2020年广东经销商预算目标
经销商代码 经销商名称 成车数量 成车金额 商品金额 客户签字
{code} {name} {number} {money} {total}
"""
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,运维)