File Operation in Python, generate pdf using reportlab
https://docs.reportlab.com/demos/hello_world/hello_world/
>python -m venv venv
> venv/scripts/activate
>pip install reportlab
crate a file in same fomder and past code from following link:
https://docs.reportlab.com/demos/hello_world/hello_world/
To run file:
>python filename.py
//////////////////////////////
# import the canvas object
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
# create a pdf document using path
my_pdf="my_report.pdf"
# creates a canvas object
c=canvas.Canvas(my_pdf,bottomup=1,pagesize=A4)
# using the pagesize to get the width and height
#default page size unit is pt 1'=72pt
w,h=int(A4[0]),int(A4[1])
print(w,h)
#we can customize page size
from reportlab.lib.units import inch, cm
# কাস্টম সাইজ ডিফাইন করা (প্রস্থ, উচ্চতা)
custom_size = (10*inch, 5*inch)
# ক্যানভাস তৈরি করার সময় pagesize আর্গুমেন্টে এটি দিয়ে দিন
c = canvas.Canvas("custom_page.pdf", pagesize=custom_size)
w,h=int(custom_size[0]),int(custom_size[1])
print(w,h)
# Draw a simple grid without any data
xlist=[20,70,120,170]
ylist=[h-20,h-70,h-120,h-170]
c.grid(xlist,ylist)
#Add data into grid
c.drawString(20,h-20,"Hello Mr Trump")
c.save()
///////////////////////////New File/////////////////
//Add data into grid
# import the canvas object
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
# create a pdf document using path
my_pdf="my_report.pdf"
# creates a canvas object
c=canvas.Canvas(my_pdf,bottomup=1,pagesize=A4)
w,h=int(A4[0]),int(A4[1])
l1=[("id","name","class","mark","gender"),
(1,"Haasan Imam Tarek","Four",78,"male"),
(2,"Jamal Khan","Five",85,"male"),
(3,"Mina Khanom","Four",78,"female")]
#from the list of data required number of rows and columns
rows,cols=len(l1),len(l1[0])
width_cell,height_cell=75,20
xlist=list(range(20,width_cell*(cols+1),width_cell))
ylist=list(range(h-20,h-(height_cell*(rows+2)),-height_cell))
c.grid(xlist,ylist)
# create the loop using number of rows and columns and add the data to the cell
for i in range(rows):
for j in range(cols):
c.drawString(xlist[j]+2,ylist[i]-int(height_cell/1.5),str(l1[i][j]))
c.save()
//////////////////////////////////////////////////////////
////Excel File to PDF
import pandas as pd
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch
def generate_pdf_from_excel(excel_file,pdf_file):
df=pd.read_excel(excel_file)
# ২. ডাটা ফরম্যাট করা (কলামের বড় লেখা হ্যান্ডেল করতে Paragraph ব্যবহার)
styles = getSampleStyleSheet()
header_style = styles["BodyText"]
data=[]
# হেডার অ্যাড করা
headers=[Paragraph(f"<b>{col}</b>",header_style) for col in df.columns]
data.append(headers)
# রোগুলো অ্যাড করা
for _, row in df.iterrows():
data.append([Paragraph(str(item),header_style) for item in row])
# ৩. পিডিএফ ডকুমেন্ট সেটআপ
doc = SimpleDocTemplate(pdf_file, pagesize=A4)
elements = []
# ৪. টেবিল তৈরি (colWidths দিয়ে কলামের প্রস্থ নির্ধারণ)
col_widths = [0.5*inch, 3*inch, 1*inch,1*inch,1*inch] # প্রয়োজনীয় কলাম অনুযায়ী পরিবর্তন করুন
t = Table(data, colWidths=col_widths)
# ৫. টেবিল স্টাইল
style = TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.cadetblue),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('GRID', (0, 0), (-1, -1), 0.5, colors.grey),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
])
t.setStyle(style)
elements.append(t)
doc.build(elements)
print(f"Success: {pdf_file} generated!")
generate_pdf_from_excel("test.xlsx", "report.pdf")
///////////////////////////////////////////////////////////////add header
Comments
Post a Comment