[Python] Python to PPTX using python-pptx library

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to update ppt using python. I have following set of code and need a help to solve following issue.

    1. Value in table should be integer with comma seperator.
    2. The columns should auto adjust as sometime figures are too big and it spill over to next row which doesn't solve the pupose of automation.
    3. The graph and table overlap, is there a way to dynamically adjust them so that irrespective of number of rows in table, graph will always be below table and not overlaping.

    from pptx import Presentation
    from pptx.util import Inches
    import pandas as pd
    import matplotlib.pyplot as plt

    path = 'C:/Users/admin/Desktop/Demo/Demo.pptx'
    # Sample data
    data = {
    'Column1': [1234.56789, 2345.67890, 3456.78901, 4567.89012, 5678.90123],
    'Column2': [9876.54321, 8765.43210, 7654.32109, 6543.21098, 5432.10987],
    'Column3': [3210.98765, 4321.09876, 5432.10987, 6543.21098, 7654.32109],
    'Column4': [7890.12345, 8901.23456, 9012.34567, 1234.56789, 2345.67890],
    'Column5': [4567.89012, 3456.78901, 2345.67890, 1234.56789, 9876.54321]
    }

    # Create a DataFrame
    df = pd.DataFrame(data)

    # Create a PowerPoint presentation
    prs = Presentation()

    # Add first slide with title
    slide = prs.slides.add_slide(prs.slide_layouts[0])
    title = slide.shapes.title
    title.text = "Analysis"

    # Add a slide for data and graph
    slide = prs.slides.add_slide(prs.slide_layouts[5]) # Layout for title and content

    # Add title for the slide
    title_shape = slide.shapes.title
    title_shape.text = "Basic Summary"

    # Add a table for the data
    left = Inches(1)
    top = Inches(1.5)
    width = Inches(8)
    height = Inches(4)

    # Create a table with rows and columns
    table = slide.shapes.add_table(6, 6, left, top, width, height).table

    # Set column widths
    table.columns[0].width = Inches(2)
    table.columns[1].width = Inches(1.5)
    table.columns[2].width = Inches(1.5)
    table.columns[3].width = Inches(1.5)
    table.columns[4].width = Inches(1.5)
    table.columns[5].width = Inches(1.5)

    # Set column titles
    table.cell(0, 0).text = ' '
    for i, col in enumerate(df.columns):
    table.cell(0, i+1).text = col

    # Populate the table with data
    for i, row in enumerate(df.itertuples(), start=1):
    for j, value in enumerate(row[1:], start=1):
    table.cell(i, j).text = f'{value:.5f}'

    # Plot the graph
    fig, ax = plt.subplots()
    df.plot(ax=ax)
    plt.xlabel('X Label')
    plt.ylabel('Y Label')
    plt.title('Graph Title')

    # Save the plot to a file
    plt.savefig('graph.png')

    # Add the graph to the slide
    left = Inches(1)
    top = Inches(5)
    pic = slide.shapes.add_picture('graph.png', left, top, width=Inches(8), height=Inches(3))

    # Save the presentation
    prs.save(path)

    # Close the plot
    plt.close()

    Continue reading...

Compartilhe esta Página