[Python] Python: For loops and Power Point Creation

Discussão em 'Python' iniciado por Stack, Fevereiro 13, 2025.

  1. Stack

    Stack Membro Participativo

    I was trying to create an automation where using it would create multiple power point in one run.

    I tried using for loops for creating the power point. I already created multiple pptx based on the for loops but the text box created using the formula inside the for loops was resulting to this (please refer to the photo)

    pptx text box result

    It kept the value of the previous iteration from the for loops. (sales_percentage_chg = SPC)

    This was happening:

    pptx A - SPC A

    pptx B - Both SPC A&B appears

    pptx C - SPC A,B & C appears ... and so on to other pptx

    My Goal was like this

    pptx A - SPC A

    pptx B - SPC B

    pptx C - SPC C ... and so on

    Here is some of the code for reference:

    import openpyxl as xl
    import pandas as pd
    from pptx import Presentation
    from pptx.util import Inches, Pt

    unique_values = df['ColumnA'].unique()
    prs = Presentation('pttx')
    slide_1 = prs.slides[0]
    slide_2 = prs.slides[1]
    left = top = width = height = Inches(1)

    for value in unique_values:
    filtered_df = df[df['ColumnA'] == value]
    unique_cat_role = filtered_df.groupby('ColumnB', as_index = False).sum('ColumnC')
    highest_cat_role = unique_cat_role.sort_values(by = 'ColumnC', ascending = False)
    top_unit_role = highest_cat_role.head(1)
    if len(top_unit_role) > 0:
    top_role = top_unit_role.iloc[0]
    highest_cat_role_new = top_role['ColumnB']
    filtered_df_new = filtered_df[filtered_df['ColumnB'] == highest_cat_role_new]
    sorted_df = filtered_df_new.sort_values(by='ColumnD', ascending=False)
    top_3 = sorted_df.head(3)
    sales_diff = (int(filtered_df_new['ColumnC'].sum())-int(filtered_df_new['ColumnE'].sum()))#/int(filtered_df_new['Sales PY'].sum())
    sales_percentage_chg = str(round((sales_diff/int(filtered_df_new['ColumnE'].sum()))*100,2)) + "%"
    textbox = slide_2.shapes.add_textbox(left,top,width,height)
    sales_percentage_chg_pptx = textbox.text_frame
    sales_percentage_chg_pptx.text = f'{str(sales_percentage_chg)}'
    prs.save(ppt_name.pptx)


    I already tried making for loop inside the for loop buy it wont work.

    I was hoping that the specific value will on appear on that specific pptx

    Continue reading...

Compartilhe esta Página