External function created to generate the PDF

This commit is contained in:
Aloma Blanch 2020-07-17 05:51:22 -05:00
parent 0ac821410c
commit ceb92aa590
3 changed files with 146 additions and 124 deletions

View File

@ -1,4 +1,10 @@
#!/usr/bin/env python
"""
Created on Thu Jul 16 15:08:27 2020
@author: Aloma Blanch
"""
import numpy as np
import matplotlib.pyplot as plt

130
generatePDF.py Normal file
View File

@ -0,0 +1,130 @@
#!/usr/bin/env python
"""
Created on Fri Jul 17 05:34:59 2020
@author: Aloma Blanch
"""
from fpdf import FPDF
def generatePDF(save_path,project,DBP,MBP,SBP,PP,Q_avg,txt1,txt2):
class PDF(FPDF):
def header(self):
# Arial bold 15
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)
def footer(self):
# Position at 1.5 cm from bottom
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 +'/periodicity.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
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 = '')
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 = '')
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.cell(0, 10,txt2, 0, 1)
pdf.output(save_path + '/' + project + '-report.pdf', 'F')

134
main.py
View File

@ -1,4 +1,9 @@
#!/usr/bin/env python
"""
Created on Thu Jul 16 15:08:27 2020
@author: Aloma Blanch
"""
import numpy as np
import matplotlib.pyplot as plt
@ -15,7 +20,9 @@ from fpdf import FPDF
from functions import error_plot, periodicity, pressure, flow, inlet_flow_waveform
from generatePDF import generatePDF
# Selct dir
Tk().withdraw()
folder = askdirectory()
@ -59,126 +66,5 @@ 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)
# # Create PDF report
class PDF(FPDF):
def header(self):
# Arial bold 15
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)
def footer(self):
# Position at 1.5 cm from bottom
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 +'/periodicity.jpg', x = None, y = None, w = 140, h = 100, type = '', link = '')
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 = '')
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 = '')
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.cell(0, 10,txt2, 0, 1)
pdf.output(save_pdf, 'F')
# Create PDF report
generatePDF(save_path,project,DBP,MBP,SBP,PP,Q_avg,txt1,txt2)