SoFunction
Updated on 2024-11-10

Python implementation of Excel content will be batch exported to PDF files

preamble

Previous articleWe have achieved a number of forms of data merged into a form, this time we have to learn how to separate the form of data exported as PDF files.

Selected data

Then you need to install the software wkhtmltopdf.

If you don't know how to download it, you can swipe on the left side of your computer to find me.

Effective demonstration

Data exported separately as a PDF

Implementation Code

import pdfkit
import openpyxl
import os

target_dir = 'Dealer budget'

if not (target_dir):
    (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">2020Annual GD Distributor Budget Goals</td>
    </tr>
    <tr>
        <td>Dealer Code</td>
        <td>Dealer Name</td>
        <td>Number of vehicles</td>
        <td>Amount of completed vehicles</td>
        <td>Amount of goods</td>
        <td>Client Signature</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 = (wkhtmltopdf='D:\\wkhtmltopdf\\bin\\')
    pdfkit.from_file(filename_html, filename_pdf, configuration=config)


wb = openpyxl.load_workbook('2020 Dealer Goals.xlsx')

sheet = wb['Sheet1']

print()

for row in list()[3:]:
    data = [ for value in row]
    data = data[1:-1]
    format_html = ('[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('', mode='w', encoding='utf-8') as f:
        (format_html)
    html_to_pdf('', target_dir +  + data[0] + " " + data[1] + '.pdf')

To this point, this article on the realization of Python Excel content batch export to PDF files is introduced to this article, more related Python Excel export to PDF content, please search for my previous articles or continue to browse the following related articles I hope that you will support me more in the future!