pythonpandasmatplotlibreport

Python One Page Report Based on Output of Function


I am trying to prepare a report using Python. So the first function produces some output and next, I run function 2 and get an output, at the end, I need all output one below the other saved in .pdf or .doc format at one location, with some header/title to the report. It's a kind of one-pager report with multiple outputs, it can be pandas data frame or pictures.

# Function 1 Output
|Columns|Values|
|-------|------|
| A     | 100  |     
| B     | 200  |
| C     | 300  |
|-------|------|

# Some other processing steps.

# Function 2 Output
|Columns|Values|
|-------|------|
| D     | 400  |     
| E     | 500  |
| F     | 600  |
|-------|------|


Solution

  • Proposed script :

    All steps commented inside

    import pandas as pd
    from reportlab.lib.pagesizes import letter
    from reportlab.pdfgen import canvas
    
    
    def function1():
        """Dataframe returned by function 1"""
        return pd.DataFrame({'Columns': ['A', 'B', 'C'], 'Values': [100, 200, 300]})
    
    
    def function2():
        """Dataframe returned by function 2"""
        return pd.DataFrame({'Columns': ['D', 'E', 'F'], 'Values': [400, 500, 600]})
    
    
    def create_report():
        """Creates PDF report"""
        # Create a new PDF document
        c = canvas.Canvas("report.pdf", pagesize=letter)
    
        # Set the title/header of the report
        c.setFont("Helvetica-Bold", 20) # sets Police
        c.drawString(50, 750, "Report Title")  # defines a title
    
        # Call function 1 and add its output to the report
        df1 = function1()
        c.setFont("Helvetica", 14) # sets Police
        c.drawString(50, 700, "Function 1 Output:") # Label for dataframe 1
        c.drawString(50, 680, str(df1)) # PDF printing
    
        # Add some spacing between function outputs
        c.drawString(50, 650, "-" * 50)
    
        # Call function 2 and add its output to the report
        df2 = function2()
        c.setFont("Helvetica", 14) # sets Police
        c.drawString(50, 620, "Function 2 Output:") # Label for dataframe 2
        c.drawString(50, 600, str(df2)) # PDF printing
    
        # Save the PDF document
        c.save()
    
    create_report()
    

    Result :

    enter image description here

    To draw a plot in PDF

    Import :

    import pandas as pd
    import matplotlib.pyplot as plt
    from reportlab.lib.pagesizes import letter
    from reportlab.pdfgen import canvas
    from reportlab.lib.utils import ImageReader
    

    In create_report function, for instance :

    ...
        # Call function 1 and create a plot of its output
        df1 = function1()
        plt.figure(figsize=(5, 3)) # set plot size
        plt.bar(df1['Columns'], df1['Values']) # create bar plot
        plt.title('Function 1 Output') # set plot title
        plt.savefig('plot1.png') # save plot as image file
    
        # Add plot to PDF
        img = ImageReader('plot1.png')
        c.drawImage(img, 50, 700, width=300, height=300)
    ...
    

    enter image description here