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

发布时间:2021-11-25 15:12:55 作者:小新
来源:亿速云 阅读:1188

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

项目名称:

将excel内容批量转化为pdf

知识点:

开发环境:

代码

导入工具

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 = """
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        table {
            font-size: 22px;
            width: 850px;
        }

        .blod {
            font-weight: bolder;
        }
    </style>
</head>
<body>
<table border="1" align="center">
    <tr>
        <td align="center" colspan="6">2020年广东经销商预算目标</td>
    </tr>
    <tr>
        <td>经销商代码</td>
        <td>经销商名称</td>
        <td>成车数量</td>
        <td>成车金额</td>
        <td>商品金额</td>
        <td>客户签字</td>
    </tr>
    <tr class="blod">
        <td>code</td>
        <td>name</td>
        <td>number</td>
        <td>money</td>
        <td>total</td>
        <td></td>
    </tr>
</table>
</body>
</html>

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

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

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

推荐阅读:
  1. 如何将PDF转Excel表格?俩种方法三秒搞定
  2. python批量将excel内容进行翻译、写入

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python excel pdf

上一篇:PHP5.5.32版本的错误机制有哪些

下一篇:PHP图片处理库Grafika怎么使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》