0

I have a python code which generates a python chart, here it is:

import pandas as pd
import plotly.express as px

data = {'Q7a':  ['Nonkopir', 'Nonkopir','Mara', 'Mara','Miami', 'Miami'],
        'Q8a': ['Littering', 'Littering','Littering', 'NAN','Littering','Littering',],
        'Q8b': ['Affect health', 'Affect health','NaN', 'Affect health','Affect health', 'Affect health',],
        'Q8c': ['NAN', 'Affect environ','Affect environ', 'Affect environ','Affect environ', 'Affect environ'],
        'Q8d': ['Others', 'NAN','Others', 'NAN','Others', 'Rodents',]
        }

waste_priority = pd.DataFrame (data, columns = ['Q7a','Q8a','Q8b','Q8c','Q8d'])

dfp = pd.concat(
    [
        waste_priority.groupby("Q7a")[c]
        .value_counts()
        .rename("counts")
        .reset_index()
        .assign(q=c)
        for c in waste_priority.columns
        if c[0:2] == "Q8"
    ]
)
dfp["q_name"] = dfp["q"].map(
    {
        "Q8a": "Littering smells and looks bad",
        "Q8b": "Effect on human health",
        "Q8c": "Effect on environment",
        "Q8d": "Others",
    }
)
fig=px.bar(
    dfp, x="Q7a", y="counts", text="counts", color="q_name", barmode="group"
).update_layout(
    title_text="Waste prioty per Estate",
    barmode="group",
    xaxis_title="",
    yaxis_title="",
    legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1, title=""),
)

Now if I want to export this challenge to power point, I have to save it first as an image with destroys quality and also it doesnt save the image of the chart in working directory, I have to find it from Downloads. This is the code I use to save the image:

offline.plot(fig3, image = 'png', image_filename='Waste_priority')

Now I used python-pptx package to export to powerpoint. challenge with this also is to manually create slides in the code if I am exporting more charts to separate slides. here is the code for creating slides:

# create an object ppt
ppt = Presentation()
 
# add a new slide
first_slide = ppt.slides.add_slide(ppt.slide_layouts[0])
 
# title (included date)
title = "Household Waste Collection Report - " + str(date.today())

# set the title on first slide
first_slide.shapes[0].text_frame.paragraphs[0].text = title


# slide 2 - set the image
img = 'Waste_storage.png'
second_slide = ppt.slide_layouts[1]
slide2 = ppt.slides.add_slide(second_slide)


# play with the image attributes if you are not OK with the height and width
pic = slide2.shapes.add_picture(img, left= Inches(2),top = Inches(1),height = Inches(6))

 
# save the powerpoint
ppt.save('Household_data_report.pptx')

I did like assistance first if there is a way I can pass a chart directly to powerpoint without saving the chart to my computer. Secondly if possible automate creation of slides other than manually coding slides(which creates repetition of code), since I have more charts to export.

LivingstoneM
  • 1,088
  • 10
  • 28
  • See my comment [here](https://stackoverflow.com/questions/73920831/print-imported-image-in-powerpoint-using-pypptx?noredirect=1#comment130532568_73920831) about [plotlyPowerpoint](https://pypi.org/project/plotlyPowerpoint/) which seems to target the title of your post. – Wayne Oct 02 '22 at 15:33

0 Answers0