PDF report generation - figures, table, text

This commit is contained in:
Aloma Blanch 2020-07-17 01:16:04 -05:00
parent 1e4ef53bc6
commit 0ac821410c
2 changed files with 118 additions and 14 deletions

View File

@ -80,7 +80,8 @@ def periodicity(project,folder,dt,T_cyc,n_cyc,save_path):
if (peak_Pdiff[-1]<=1): if (peak_Pdiff[-1]<=1):
print('The numerical simulation \'{0}\' has achieve periodicity!\nSystolic Blood Pressure (SBP):\nsecond-last cycle = {1:.2f} mmHg,\nlast cycle = {2:.2f} mmHg,\n\u0394mmHg = {3:.2f} mmHg'.format(project,peak_P[-2],peak_P[-1],peak_Pdiff[-1])) print('The numerical simulation \'{0}\' has achieve periodicity!\nSystolic Blood Pressure (SBP):\nsecond-last cycle = {1:.2f} mmHg,\nlast cycle = {2:.2f} mmHg,\n\u0394mmHg = {3:.2f} mmHg'.format(project,peak_P[-2],peak_P[-1],peak_Pdiff[-1]))
txt = 'The numerical simulation \'{0}\' has achieve periodicity!\nSystolic Blood Pressure (SBP):\nsecond-last cycle = {1:.2f} mmHg,\nlast cycle = {2:.2f} mmHg,\nDelta_mmHg = {3:.2f} mmHg'.format(project,peak_P[-2],peak_P[-1],peak_Pdiff[-1]) # txt = ['The numerical simulation \'{0}\' has achieve periodicity!'.format(project), 'Systolic Blood Pressure (SBP):', 'Second-last cycle = {0:.2f} mmHg'.format(peak_P[-2]), 'Last cycle = {0:.2f} mmHg'.format(peak_P[-1]), 'Delta_mmHg = {0:.2f} mmHg'.format(peak_Pdiff[-1])]
txt = ['The numerical simulation \'{0}\' has achieve periodicity!'.format(project), 'Systolic Blood Pressure (SBP):','Second-last cycle = {0:.2f} mmHg - Last cycle = {1:.2f} mmHg - Delta_mmHg = {2:.2f} mmHg'.format(peak_P[-2],peak_P[-1],peak_Pdiff[-1])]
return txt return txt
def pressure(folder,N_ts,T_cyc,dt,n_cyc,save_path): def pressure(folder,N_ts,T_cyc,dt,n_cyc,save_path):

129
main.py
View File

@ -22,7 +22,7 @@ folder = askdirectory()
project_folder = os.path.dirname(folder) project_folder = os.path.dirname(folder)
project = os.path.basename(project_folder) project = os.path.basename(project_folder)
save_path = folder+'/'+project+'-report' save_path = folder+'/'+project+'-report'
# os.mkdir(save_path) os.mkdir(save_path)
save_pdf = save_path + '/' + project + '-report.pdf' save_pdf = save_path + '/' + project + '-report.pdf'
# Input parameters # Input parameters
@ -59,23 +59,126 @@ txt1 = periodicity(project,folder,dt,T_cyc,n_cyc,save_path)
txt2 = inlet_flow_waveform(project_folder,t_btw_rst,N_ts,dt,T_cyc,n_cyc,save_path) txt2 = inlet_flow_waveform(project_folder,t_btw_rst,N_ts,dt,T_cyc,n_cyc,save_path)
# Create PDF report # # Create PDF report
pdf = FPDF('P','mm','Letter') class PDF(FPDF):
pdf.add_page() def header(self):
pdf.set_font('Times', 'B', 18) # Arial bold 15
pdf.cell(200, 20, 'Project name: '+ project, 0, 1, 'C') self.set_font('Times', 'B', 15)
# Calculate width of title and position
w = self.get_string_width(title) + 6
self.set_x((210 - w) / 2)
# Colors of frame, background and text
self.set_draw_color(211, 84, 0 )
self.set_fill_color(249, 231, 159)
self.set_text_color(40, 116, 166)
# Thickness of frame (0.2 mm)
self.set_line_width(0.2)
# Title
self.cell(w, 9, title, 1, 1, 'C', 1)
# Line break
self.ln(10)
# Keep this ratio - 5.42/3.86 def footer(self):
pdf.set_font('Times', '', 14) # Position at 1.5 cm from bottom
pdf.cell(200, 30, 'Cehcking convergency and periodicity:', 0, 1, 'L') self.set_y(-15)
# Arial italic 8
self.set_font('Times', 'I', 8)
# Text color in gray
self.set_text_color(128)
# Page number
self.cell(0, 10, 'Page ' + str(self.page_no()), 0, 0, 'C')
def chapter_title(self, num, label):
# Arial 12
self.set_font('Times', '', 12)
# Background color
self.set_fill_color(200, 220, 255)
# Title
self.cell(0, 6, 'Chapter %d : %s' % (num, label), 0, 1, 'L', 1)
# Line break
self.ln(4)
def section_title(self, num, label):
# Arial 12
self.set_font('Times', '', 12)
# Background color
self.set_fill_color(232, 248, 245)
# Title
self.cell(0, 6, 'Section %d : %s' % (num, label), 0, 1, 'L', 1)
# Line break
self.ln(4)
def chapter_body(self, name):
# Read text file
with open(name, 'rb') as fh:
txt = fh.read().decode('latin-1')
# Times 12
self.set_font('Times', '', 12)
# Output justified text
self.multi_cell(0, 5, txt)
# Line break
self.ln()
# Mention in italics
self.set_font('', 'I')
self.cell(0, 5, '(end of excerpt)')
def print_chapter(self, num, title, name):
self.add_page()
self.chapter_title(num, title)
self.chapter_body(name)
def print_section(self, num, title):
self.add_page()
self.section_title(num, title)
title = 'Project name: '+ project
pdf = PDF()
pdf.set_title(title)
pdf.set_author('Aloma Blanch Granada')
pdf.print_section(1, 'Cehcking convergency and periodicity')
pdf.image(save_path +'/Log_Last_nonlin_res_error.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '') pdf.image(save_path +'/Log_Last_nonlin_res_error.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
pdf.image(save_path +'/periodicity.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '') pdf.image(save_path +'/periodicity.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
pdf.cell(200, 20, 'Cehcking Pressures at each outlet:', 0, 1, 'L') pdf.cell(0, 10,txt1[0], 0, 1)
pdf.cell(0, 10,txt1[1], 0, 1)
pdf.cell(0, 10,txt1[2], 0, 1)
pdf.print_section(2, 'Cehcking Pressures at each outlet')
pdf.image(save_path +'/pressure.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '') pdf.image(save_path +'/pressure.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
pdf.cell(200, 20, 'Cehcking Flow Rate at each outlet:', 0, 1, 'L')
width_cell=[20,30,30,30,30,30,30];
# pfd.SetFillColor(193,229,252); # Background color of header
# Header starts
pdf.cell(width_cell[0],10,'ROI',1,0,'C') # First header column
pdf.cell(width_cell[1],10,'DBP [mmHg]',1,0,'C') # Second header column
pdf.cell(width_cell[2],10,'MBP [mmHg]',1,0,'C') # Third header column
pdf.cell(width_cell[3],10,'SBP [mmHg]',1,0,'C') # Fourth header column
pdf.cell(width_cell[4],10,'PP [mmHg]',1,1,'C') # Fourth header column
# Rows
for i in range(0,len(SBP)):
pdf.cell(width_cell[0],10,'ROI-'+ str(i+2),1,0,'C') # First column of row 1
pdf.cell(width_cell[1],10,str(round(DBP[i],2)),1,0,'C') # Second column of row 1
pdf.cell(width_cell[2],10,str(round(MBP[i],2)),1,0,'C') # Third column of row 1
pdf.cell(width_cell[3],10,str(round(SBP[i],2)),1,0,'C') # Fourth column of row 1
pdf.cell(width_cell[4],10,str(round(PP[i],2)),1,1,'C') # Fourth column of row 1
pdf.print_section(3, 'Cehcking Flow Rate at each outlet')
pdf.image(save_path +'/flow.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '') pdf.image(save_path +'/flow.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
width_cell=[20,60];
# pfd.SetFillColor(193,229,252); # Background color of header
# Header starts
pdf.cell(width_cell[0],10,'ROI',1,0,'C') # First header column
pdf.cell(width_cell[1],10,'Average Flow rate [mL/s]',1,1,'C') # Second header column
# Rows
for i in range(0,len(Q_avg)):
pdf.cell(width_cell[0],10,'ROI-'+ str(i+2),1,0,'C')
pdf.cell(width_cell[1],10,str(round(Q_avg[i],2)),1,1,'C')
pdf.print_section(4, 'Cehcking Inlet Flow Waveform and time steps saved')
pdf.image(save_path +'/inlet_waveform.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '') pdf.image(save_path +'/inlet_waveform.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
pdf.cell(0, 10,txt2, 0, 1)
pdf.output(save_pdf, 'F') pdf.output(save_pdf, 'F')