How to paste data-frame directly to powerpoint using python-pptx module

2.1k Views Asked by At

I have a data-farme which looks like:

          yyyymm      A_growth         B_growth
         201911        NaN              NaN
         201912       -1.0             -3.0
         202001       14.0             20.0
         202002        7.0              9.0
         202003       -2.0            -14.0
         202004      -44.0            -44.0
         202005       20.0              6.0
         202006       24.0             27.0
         202007       -7.0             -1.0

I want to directly export it to power point at bottom right of slide as a small table.

One way is to create table and then use for loops to fill each cell in the table which is time consuming.

How can I use python-pptx module to directly export this table to power point.?

I am using blank_slide_layout = prs.slide_layouts[6] layout 6.

1

There are 1 best solutions below

0
On BEST ANSWER

You can use the add_table() function: (see docs)

df = pd.DataFrame({'hi': [1, 2, 3], 'there': ['a', 'b', 'c']})
x, y, cx, cy = Inches(2), Inches(2), Inches(4), Inches(1.5)
shape = slide.shapes.add_table(3, 3, x, y, cx, cy)
table = shape.table
cell = table.cell(1, 2)
cell.text = str(df.iloc[1][2])