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


    前言

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

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

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

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

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

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

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

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

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

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

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

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

    部分数据

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

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

    效果展示

    代码实现

    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')
    • 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

    尾语

    要成功,先发疯,下定决心往前冲!

    学习是需要长期坚持的,一步一个脚印地走向未来!

    未来的你一定会感谢今天学习的你。

    —— 心灵鸡汤

    本文章到这里就结束啦~感兴趣的小伙伴可以复制代码去试试哦 😝

    👇问题解答 · 源码获取 · 技术交流 · 抱团学习请联系👇
  • 相关阅读:
    OVS 和 OVS-DPDK 对比
    Threejs中使用Tweenjs实现动画效果和Tweenjs使用说明文档
    商城详情页的开发
    AI 边缘计算平台 - 爱芯元智 AX620A 爱芯派开箱
    聊聊自制的探索大全扑克牌
    从 QWebEnginePage 打印文档
    python 第五章
    ASP.NET Core 6框架揭秘实例演示[27]:ASP.NET Core 6 Minimal API的模拟实现
    单个数据盘分区如何配置LVM
    Java Swing 飞机大战游戏
  • 原文地址:https://blog.csdn.net/python56123/article/details/127452455