from reportlab.pdfgen import canvas
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'
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 table_pandas_data import my_data # import the data
my_doc = SimpleDocTemplate(my_path,pagesize=letter)
c_width=[1*inch] # width of the columns
t=Table(my_data,colWidths=c_width,repeatRows=1)
t.setStyle(TableStyle([('FONTSIZE',(0,0),(-1,-1),12),
('BACKGROUND',(0,0),(-1,0),colors.lightgreen),('VALIGN',(0,0),(-1,0),'TOP')]))
elements=[]
elements.append(t)
my_doc.build(elements)
Basics of adding Table to PDF fileimport pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],
'MATH':[80,40,70,70,60,30],
'ENGLISH':[80,70,40,50,60,30]}
df = pd.DataFrame(data=my_dict) # dataframe
my_data=df.values.tolist() # create a list using Dataframe
Here the last line creates our required data source my_data as a list.
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
db_file='G:\\My drive\\testing\\my_db.db'
file1='sqlite:///'+ db_file
my_conn = create_engine(file1)
#my_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_tutorial")
try:
df = pd.read_sql("SELECT * FROM student",my_conn)
my_data=df.values.tolist() # add rows as list.
my_data.insert(0,df.columns) # add the columns at first row
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print(error)
tolist() is used to create List from DataFrame
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
Full student table with SQL Dump for MySQL database.Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.