To create PDF in python there are many approaches. pdfkit is one of the better approaches. It renders HTML into PDF with various image formats, HTML forms, and other complex printable documents.
Install package "pdfkit" in python using anaconda prompt.
pip install pdfkit
To make pdfkit work perfectly install wkhtmltopdf which deals with images and other complex things.To download wkhtmltopdf follow the link: http://wkhtmltopdf.org/.
Import the data and generate a pivot table as well as some summary statistics of the average quantity and price of the CPU and Software sales.
Convert "basic_salary_1_P2.csv" data into excel (.xlsx) format.
# import pandas to create dataframe
import pandas as pd
# import pdfkit to convert the file into pdf
import pdfkit
# read the excel file as a dataframe
df = pd.read_excel('basic_salary_1_P2.xlsx', index_col=0)
df.head()
Create a html file first in the folder where your code is present.
Convert the dataframe into html code and write it to the html file created earlier.
f = open('exp.html','w')
a = df.to_html()
f.write(a)
f.close()
As wkhtmltopdf software was installed earlier. Add the path to the folder in the environment. Copy this wkhtmltopdf folder to the folder where your code is present. Then convert the html file into a pdf file using:
pdfkit.from_file('exp.html', 'example.pdf')