Python如何將excel內(nèi)容批量轉(zhuǎn)化為pdf

這篇文章給大家分享的是有關(guān)Python如何將excel內(nèi)容批量轉(zhuǎn)化為pdf的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比潢川網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式潢川網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋潢川地區(qū)。費(fèi)用合理售后完善,十余年實體公司更值得信賴。

項目名稱:

將excel內(nèi)容批量轉(zhuǎn)化為pdf

知識點:

  • python 操作 excel

  • python 操作 pdf

  • html

開發(fā)環(huán)境:

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

  • 編輯器: pycharm 社區(qū)版

代碼

導(dǎo)入工具

import openpyxl
import pdfkit

加載一個本地文件

workbook = openpyxl.load_workbook('2020經(jīng)銷商目標(biāo).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的轉(zhuǎn)化軟件

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

from_file 文件的名字 保存文件的名字 轉(zhuǎn)化軟件的配置

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

轉(zhuǎn)化為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年廣東經(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 class="blod">
        <td>code</td>
        <td>name</td>
        <td>number</td>
        <td>money</td>
        <td>total</td>
        <td></td>
    </tr>
</table>
</body>
</html>

最后運(yùn)行代碼,效果如下圖

Python如何將excel內(nèi)容批量轉(zhuǎn)化為pdf

感謝各位的閱讀!關(guān)于“Python如何將excel內(nèi)容批量轉(zhuǎn)化為pdf”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

文章標(biāo)題:Python如何將excel內(nèi)容批量轉(zhuǎn)化為pdf
本文URL:http://muchs.cn/article30/geccpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司靜態(tài)網(wǎng)站、做網(wǎng)站、網(wǎng)站導(dǎo)航外貿(mào)網(wǎng)站建設(shè)、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計