欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)將Excel內(nèi)容批量導(dǎo)出為PDF文件

 更新時(shí)間:2022年04月16日 09:37:15   作者:嗨學(xué)編程  
這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)將Excel表格內(nèi)容批量導(dǎo)出為PDF文件,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的小伙伴可以了解一下

序言

上一篇咱們實(shí)現(xiàn)了多個(gè)表格數(shù)據(jù)合并到一個(gè)表格,本次咱們來學(xué)習(xí)如何將表格數(shù)據(jù)分開導(dǎo)出為PDF文件。

部分?jǐn)?shù)據(jù)

然后需要安裝一下這個(gè)軟件 wkhtmltopdf

不知道怎么下載的可以在電腦端左側(cè)掃一下找到我要

效果展示

數(shù)據(jù)單獨(dú)導(dǎo)出為一個(gè)PDF

實(shí)現(xiàn)代碼

import pdfkit
import openpyxl
import os

target_dir = '經(jīng)銷商預(yù)算'

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年廣東經(jīng)銷商預(yù)算目標(biāo)</td>
    </tr>
    <tr>
        <td>經(jīng)銷商代碼</td>
        <td>經(jīng)銷商名稱</td>
        <td>成車數(shù)量</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經(jīng)銷商目標(biāo).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')

到此這篇關(guān)于Python實(shí)現(xiàn)將Excel內(nèi)容批量導(dǎo)出為PDF文件的文章就介紹到這了,更多相關(guān)Python Excel導(dǎo)出為PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論