Convert a multiple CSV files into a PDF with Python

Viewed 21

Hello everyone I'm trying to convert a multiple tables to a PDF. I have several CSV files and i already read them and i get a list with multiples DataFrame (tables) but now I'm not able to get each Table and convert them first into a HTML and then into PDF. is there any Advice?

with to_html() from pandas i can't convert a list with multiple DataFrames. maybe there is a way to get first every DataFrame and the convert each one into a HTML? thanks a lot. My code is below.

import markdown2 # pip install markdown2
import os
import pdfkit  # for pdf output (uses wkhtml2pdf, which must be in your PATH)
import pandas as pd
import glob

#to install wkhtmltopdf-have to put the path where the .exe format is 
path_wkhtmltopdf = r"C:\Users\Downloads\wkhtmltox-0.12.6-1.mxe-cross-win64\wkhtmltox\bin\wkhtmltopdf.exe"
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)

#to get the folder where the tables in .csv format are
path_folder = r"I:\Jorge\Tables\Tables CSV"
dir_list = os.listdir(path_folder)
#print (dir_list)

filenames = glob.glob(path_folder + "\*.csv")
#print(filenames)

all_tables = []
for f in filenames:
    #print(f'loading {f}')
    ne_df = pd.read_csv(f)
    all_tables.append(ne_df)
    #print(all_tables)


        
#Configurations for the output PDF, 
options = {
    'page-size': 'A4',
    'margin-top': '0.90in',
    'margin-right': '0.90in',
    'margin-bottom': '0.90in',
    'margin-left': '0.90in',
    'encoding': "UTF-8",
    'custom-header': [
        ('Accept-Encoding', 'gzip')
    ],
    'cookie': [
        ('cookie-empty-value', '""'),
        ('cookie-name1', 'cookie-value1'),
        ('cookie-name2', 'cookie-value2'),
    ],
    'no-outline': None
}

#vonvert the html into a pdf
out = pdfkit.from_string(ToHtml, "test1.1.pdf", configuration=config, options=options )
0 Answers
Related