网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Python如何将excel内容批量转化为pdf

这篇文章给大家分享的是有关Python如何将excel内容批量转化为pdf的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

创新互联公司-专业网站定制、快速模板网站建设、高性价比潢川网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式潢川网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖潢川地区。费用合理售后完善,十余年实体公司更值得信赖。

项目名称:

将excel内容批量转化为pdf

知识点:

  • python 操作 excel

  • python 操作 pdf

  • html

开发环境:

  • 解释器: Python 3.6.5 | Anaconda, Inc.

  • 编辑器: pycharm 社区版

代码

导入工具

import openpyxl
import pdfkit

加载一个本地文件

workbook = openpyxl.load_workbook('2020经销商目标.xlsx')

def func(money):
    # print(money)
    str_number = f'{money}'
    str_list = list(str_number[::-1])
    str_ = ""
    i = 1
    for no, char in enumerate(str_list):
        str_ += char
        # print(no, char)
        if i % 3 == 0 and len(str_list) != i:
            str_ += ','
        i += 1
    # print(str_[::-1])
    return str_[::-1]

sheet1 = workbook['Sheet1']
print(sheet1)
for row in list(sheet1.rows)[3:]:
    for col in row[1:]:
        print(col.value, end='\t')
    code = row[1].value
    name = row[2].value
    number = row[3].value
    money = row[4].value
    total = row[5].value
    print(code, name, number, money)


    str_number = func(number)
    str_money = func(money)
    str_total = func(total)
    html = html.replace('code', code)
    html = html.replace('name', name)
    html = html.replace('number', f'{str_number}.00')
    html = html.replace('money', f'{str_money:}.00')
    html = html.replace('total', f'{str_total}.00')
    # print(html)
    with open('example.html', mode='w', encoding='utf-8') as f:
         f.write(html)

配置pdf的转化软件

config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')

from_file 文件的名字 保存文件的名字 转化软件的配置

pdfkit.from_file('example.html', f'{name}.pdf', configuration=config)

转化为PDF

html = """



    
    Document
    



    
        2020年广东经销商预算目标
    
    
        经销商代码
        经销商名称
        成车数量
        成车金额
        商品金额
        客户签字
    
    
        code
        name
        number
        money
        total
        
    


最后运行代码,效果如下图

Python如何将excel内容批量转化为pdf

感谢各位的阅读!关于“Python如何将excel内容批量转化为pdf”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


网页标题:Python如何将excel内容批量转化为pdf
当前路径:http://bjjierui.cn/article/geccpo.html

其他资讯