Adding tabular data to PDF file by adding table with option and styles using Python report lab
Adding table to PDF file
Importing libraries and setting the path to create pdf file.
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table,TableStyle,colors
#from my_table_data import my_data # import the data
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'#path,file name
data
The name of the variable is maintained as the sources can be changed. Note the variable name my_data here.
When we are not sure about the number of columns, then we can fix a common width for all the columns of our table.
c_width=[1*inch]
setStyle()
BACKGROUND , FONTSIZE are set here.
(0,0) : ( column=0,row=0) Top left cell (-1,0) : ( column=-1,row=0) Top row, column=-1 is the right most column. (-1,-1): (column=-1,row=-1) Bottom row right most ( column ) cell.
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table,TableStyle,colors
#from my_table_data import my_data # import the data
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf' # change path, file name
my_data= [['ID', 'Name', 'Class', 'Mark', 'Gender'],
(1, 'John Deo', 'Four', 75, 'female'),
(2, 'Max Ruin', 'Three', 85, 'male'),
(3, 'Arnold', 'Three', 55, 'male')]
my_doc=SimpleDocTemplate(my_path,pagesize=letter)
c_width=[0.4*inch,1.5*inch,1*inch,1*inch,1*inch]
t=Table(my_data,rowHeights=20,repeatRows=1,colWidths=c_width)
t.setStyle(TableStyle([('BACKGROUND',(0,0),(-1,0),colors.lightgreen),
('FONTSIZE',(0,0),(-1,-1),10)]))
elements=[]
elements.append(t)
my_doc.build(elements)
Using data file
In above code we created one list using some data ( column names and rows of data ) . This variable my_data we can get from different file here.
Importing the data file
Here by changing the data file we can collect data from different sources. In all our sources, after fetching the data finally we will create the list of data using the same variable name my_data.
from my_table_data import my_data
We will remove the list storing the data as we are getting the same variable ( my_data ) from another file my_table_data.py
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
db_file='G:\\My drive\\testing\\my_db.db' # change your path
my_data=[]
try:
file1='sqlite:///'+ db_file
#my_conn = create_engine(file1)
# For MySQL use the below line and remove the above lines for SQLite file
my_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_tutorial")
r_set=my_conn.execute("SELECT * FROM student ")
my_data.append(list(r_set.keys())) # add the column names
for row in r_set.fetchall():
my_data.append(row) # adding one row
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print(error)
Using MySQL database
In above code we can create the connection object my_conn by using this connection string.
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")