Skip to content

Instantly share code, notes, and snippets.

@mzmmoazam
Created January 25, 2017 18:51
Show Gist options
  • Save mzmmoazam/2dcd1ae22d66780989475019cc6dd6fa to your computer and use it in GitHub Desktop.
Save mzmmoazam/2dcd1ae22d66780989475019cc6dd6fa to your computer and use it in GitHub Desktop.
To convert csv to pdf reports using Reportlab.
import csv
from reportlab.platypus import Paragraph,SimpleDocTemplate
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.graphics.charts.piecharts import Pie
from reportlab.lib.colors import brown,blue
from reportlab.graphics.charts.legends import Legend
from reportlab.graphics.shapes import Drawing
from reportlab.lib.colors import black
# THIS FUNCTION CALCULATES THE PERCENTILE
def get_percentile(data):
c=-1
for i in per:
if i<=data:
c+=1
return c/len(per)*100
# TEXT FUNCTION DEALS WITH THE TEXT IN THE PDF
def text(data):
Title = Paragraph("Interview Report", styles["Heading1"])
# HERE THE USUAL ''.format() DOESN'T WORK .I TRIED <PRE> TAG BUT THAT ALSO DIDN'T WORK .SO THIS IS DONE IN THE TRADITIONAL WAY
Name = Paragraph("<p>"+"<br/>"*3+"<b>Name"+"&nbsp;"*(30-len("Name"))+"</b>"+data[1]+"</p>", styles["BodyText"])
email=Paragraph("<p>"+"<br/>"*1+"<b>Email"+"&nbsp;"*(30-len("email"))+"</b> "+data[2]+"</p>",styles["Normal"])
organisation=Paragraph("<p>"+"<br/>"*1+"<b>Organisation"+"&nbsp;"*(30-len("organisation"))+"</b>"+"FFI"+"</p>",styles["Normal"])
designation=Paragraph("<p>"+"<br/>"*1+"<b>Designation"+"&nbsp;"*(30-len("designation"))+"</b> "+"Abc"+"</p>",styles["Normal"])
job_details=Paragraph("<p>"+"<br/>"*1+"<b>Job Details"+"&nbsp;"*(29-len("job_details"))+"</b>"+"aaa"+"</p>",styles["Normal"])
password=Paragraph("<p>"+"<br/>"*1+"<b>Password"+"&nbsp;"*(30-len("password"))+"</b>"+"****"+"</p>",styles["Normal"])
primary_skills=Paragraph("<p>"+"<br/>"*1+"<b>Primary Skills"+"&nbsp;"*(28-len("primary_skills"))+"</b>"+"Python"+"</p>",styles["Normal"])
Start_date=Paragraph("<p>"+"<br/>"*1+"<b>Start Date"+"&nbsp;"*(31-len("Start_date"))+"</b>"+"31/1/2012"+"</p>",styles["Normal"])
Expiry_date = Paragraph("<p>"+"<br/>"*1+"<b>Expiry Date"+"&nbsp;"*(28-len("Expiry_date"))+" </b>" + "31/1/2019"+"</p>",styles["Normal"])
# APPENDING COMPONENTS TO THE LIST
for i in [Title,Name,email,organisation,designation,job_details,password,primary_skills,Start_date,Expiry_date]:
Elements.append(i)
# THIS FUNCTION CREATES THE CHART
def PieChart(data):
x=get_percentile(data) # HERE WE ASSIGN X EQUAL TO THE PERCENTILE OF THAT INDIVIDUAL
d = Drawing(250,200)
pc = Pie() # CREATING PIE CHART
pc.x = 65
pc.y = 15
pc.width = 130
pc.height = 130
pc.data = [100-x,x] # REST,YOUR PERCENTILE
pc.slices.strokeWidth=0.5
pc.slices[1].popout = 3
pc.slices[1].fillColor= blue
pc.slices[0].fillColor=brown
pc.slices[1].fontColor = black
lgnd=Legend() # CREATING LEGEND FOR THE PIE CHART
lgnd.x = 230
lgnd.y = 250/2
lgnd.dx = 8
lgnd.dy = 8
lgnd.fontName = 'Helvetica'
lgnd.fontSize = 12
lgnd.boxAnchor = 'w'
lgnd.columnMaximum = 10
lgnd.strokeWidth = 1
lgnd.strokeColor = black
lgnd.deltax = 75
lgnd.deltay = 10
lgnd.autoXPadding = 5
lgnd.yGap = 0
lgnd.dxTextSpace = 5
lgnd.alignment = 'right'
lgnd.dividerLines = 1|2|4
lgnd.dividerOffsY = 4.5
lgnd.subCols.rpad = 30
lgnd.colorNamePairs = [( blue,"Your percentile :"+str(round(x,2)))]
d.add(lgnd)
d.add(pc)
Elements.append(d)
# THIS FUNCTION CALLS OTHER FUNCTION THAT CREEATE THE FULL PDF
def go(data):
doc = SimpleDocTemplate("PDFReports/"+data[1]+'.pdf')
text(data)
PieChart(int(data[3]))
doc.build(Elements)
if __name__ == '__main__':
per = [] #LIST FOR ALL THE MARKS
styles = getSampleStyleSheet()
Elements = [] # LIST TO CONTAIN OTHER COMPONENTS
with open("sampleCSV.csv") as c:
data=list(csv.reader(c))
# CREATING THE LIST FOR ALL THE MARKS
for i in data:
if i[3]!='Score' and i[0]:
per.append(int(i[3]))
# CREATING PDF FOR ALL THE INDIVIDUALS
for i in data:
if i[3] != 'Score' and i[0]:
go(i)
@goog
Copy link

goog commented Sep 17, 2019

hello, could you share the sampleCSV.csv?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment