Created
July 21, 2016 18:48
-
-
Save rms1000watt/efb773658fe13955b7f661239183f0d8 to your computer and use it in GitHub Desktop.
Python to generate PDF report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fpdf import FPDF | |
PORTRAIT_FULL_WIDTH = 190 | |
LANDSCAPE_FULL_WIDTH = 277 | |
MAX_ROWS = 57 | |
LEFT_MARGIN = 10.00125 | |
pdf = None | |
def main(): | |
global pdf | |
pdf = PDF() | |
page1Title = 'Sheet 1 Title' | |
page1Data = [] | |
page1Columns = ['FName', 'LName', 'ID'] | |
page2Title = 'Sheet 2 Title' | |
page2Data = [] | |
page2Columns = ['FName', 'LName', 'ID'] | |
page3Title = 'Sheet 3 Title' | |
page3Data = [] | |
page3Columns = ['FName', 'LName', 'ID', 'Age'] | |
for i in range(100): | |
page1Data.append(['Jack', 'Smith', str(i)]) | |
page2Data.append(['John', 'Smith', str(i)]) | |
page3Data.append(['Joe', 'Smith', str(i), '88']) | |
handleSheet(page1Title, page1Columns, page1Data, True) | |
handleSheet(page2Title, page2Columns, page2Data, True) | |
pdf.output('report_2.pdf', 'F') | |
def handleSheet(sheetTitle, columnNamesArr, dataArr, noRepeatCol0=False): | |
global pdf | |
pdf.add_page('L') | |
pdf.set_font('Arial', '', 12) | |
col0Val = '' | |
ind = 0 | |
# for i in range(1, 300): | |
for row in dataArr: | |
if ind == MAX_ROWS: | |
ind = 0 | |
# Add page title | |
if not ind: | |
pdf.set_font_size(12) | |
pdf.set_fill_color(255,255,255) | |
pdf.set_text_color(0,0,0) | |
pdf.cell(LANDSCAPE_FULL_WIDTH, 6, sheetTitle, 0, 0, 'C') | |
pdf.ln(8) | |
# Add table header | |
if not ind: | |
pdf.set_font_size(7) | |
pdf.set_fill_color(100,100,100) | |
pdf.set_text_color(255,255,255) | |
cellInd = 0 | |
for columnName in columnNamesArr: | |
cellWidth = 150 if not cellInd else 50 | |
justification = 'L' if not cellInd else 'C' | |
pdf.cell(cellWidth, 3, columnName, 1, 0, justification, True) | |
cellInd += 1 | |
pdf.ln() | |
pdf.set_x(LEFT_MARGIN) | |
# Add table content | |
if ind: | |
pdf.set_font_size(7) | |
pdf.set_text_color(0,0,0) | |
fillColor = [240, 240, 242] if ind%2 else [255, 255, 255] | |
pdf.set_fill_color(fillColor[0], fillColor[1], fillColor[2]) | |
cellInd = 0 | |
for cell in row: | |
cellWidth = 150 if not cellInd else 50 | |
justification = 'L' if not cellInd else 'C' | |
if cell == col0Val: | |
pdf.cell(cellWidth, 3, '', 1, 0, justification, True) | |
else: | |
pdf.cell(cellWidth, 3, cell, 1, 0, justification, True) | |
if not cellInd: | |
col0Val = cell | |
cellInd += 1 | |
pdf.ln() | |
pdf.set_x(LEFT_MARGIN) | |
ind += 1 | |
class PDF(FPDF): | |
# def header(self): | |
# self.set_font('Arial', '', 13) | |
# self.cell(277, 10, 'Title', 0, 0, 'C', False) | |
# self.ln(10) | |
# Page footer | |
def footer(self): | |
# Position at 1.5 cm from bottom | |
self.set_y(-15) | |
# Arial italic 8 | |
self.set_font('Arial', 'I', 8) | |
# Page number | |
self.cell(0, 10, str(self.page_no()), 0, 0, 'C') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment