zoukankan      html  css  js  c++  java
  • 同事将excel数据转化为pdf,提前下班了,而我还在苦逼地做表

    前言

    自动化办公:

    走上杂务工作的快车道,当你实现一部分的工作自动化以后,你就会走上一个“工作自由”的快车道,因为你有更多的时间能用来研究如何去更高效的完成余下的那堆工作,然后,每天工作的时间就会越来越短。

    项目名称:

    将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='	')
        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;
                 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>

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

    同事将excel数据转化为pdf,提前下班了,而我还在苦逼地做表

     

    同事将excel数据转化为pdf,提前下班了,而我还在苦逼地做表

     

    PS:如有需要Python学习资料的小伙伴可以加下方的群去找免费管理员领取

    同事将excel数据转化为pdf,提前下班了,而我还在苦逼地做表

     

    可以免费领取源码、项目实战视频、PDF文件等

    同事将excel数据转化为pdf,提前下班了,而我还在苦逼地做表
  • 相关阅读:
    URAL 1998 The old Padawan 二分
    URAL 1997 Those are not the droids you're looking for 二分图最大匹配
    URAL 1995 Illegal spices 贪心构造
    URAL 1993 This cheeseburger you don't need 模拟题
    URAL 1992 CVS
    URAL 1991 The battle near the swamp 水题
    Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
    Codeforces Beta Round #7 D. Palindrome Degree hash
    Codeforces Beta Round #7 C. Line Exgcd
    Codeforces Beta Round #7 B. Memory Manager 模拟题
  • 原文地址:https://www.cnblogs.com/hhh188764/p/13508902.html
Copyright © 2011-2022 走看看