Get the App
SLTechnology News&Howtos  ›  Development  › 

How to achieve batch Export of Excel content to PDF File by Python

Shulou Source: shulou.com Published: 2022-05-31 21:52:25 10月04日 Update

This article introduces the knowledge of "how to export Excel content to PDF files in batch by Python". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preface

Partial data

Then you need to install the software wkhtmltopdf.

Effect display

Data is exported separately to a PDF

Implementation code import pdfkitimport openpyxlimport ostarget_dir = 'dealer budget' if not os.path.exists (target_dir): os.mkdir (target_dir) html = "" table {font-size: 22px; font-weight: bolder; width: 850px Guangdong dealer budget target dealer code dealer name car quantity amount of goods customer signature [code] {name} {money} {total} "" 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 dealer target .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')" how to achieve batch export of Excel content to PDF files by Python "ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Distributor content file code data more goal knowledge amount budget utility achievement next name commodity dilemma reality customer preface situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi Microsoft Apple OPPO Reno