Python怎么实现将Excel内容批量导出为PDF文件

发布时间:2022-04-16 10:56:00 作者:iii
来源:亿速云 阅读:305

本篇内容介绍了“Python怎么实现将Excel内容批量导出为PDF文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

序言

部分数据

Python怎么实现将Excel内容批量导出为PDF文件

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

效果展示

Python怎么实现将Excel内容批量导出为PDF文件

数据单独导出为一个PDF

Python怎么实现将Excel内容批量导出为PDF文件

实现代码

import pdfkit
import openpyxl
import os

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内容批量导出为PDF文件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 使用python怎么将字典列表导出为Excel文件
  2. vue+element表格导出为Excel文件

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

python excel pdf

上一篇:移动端调试神器vConsole怎么使用

下一篇:基于Python+Matplotlib怎么实现直方图的绘制

相关阅读

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

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