Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How Python converts excel content into pdf in batches

Shulou Source: shulou.com Published: 2022-06-01 19:53:10 09月30日 Update

This article is about how Python converts excel content into pdf content in batches. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Project name:

Batch conversion of excel content to pdf

Knowledge points:

Python operation excel

Python operation pdf

Html

Development environment:

Interpreter: Python 3.6.5 | Anaconda, Inc.

Editor: pycharm Community Edition

Code

Import tool

Import openpyxlimport pdfkit

Load a local file

Workbook = openpyxl.load_workbook ('2020 dealer target .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)

Configure the conversion software for pdf

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

The name of the from_file file saves the name of the file and the configuration of the conversion software

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

Convert to PDF

Html = "" Document table {font-size: 22px; width: 850px;} .blod {font-weight: bolder 2020 Guangdong dealer budget target dealer code dealer name car quantity amount of goods customer signature code name number money total

Finally, run the code, and the effect is as follows

Thank you for reading! This is the end of this article on "how Python converts excel content into pdf in batches". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Content dealer code file name name more goal knowledge article software amount configuration good practical commodity customer tool effect quantity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Docker vpn Shulou Tech Info