Prashant Kumar
Prashant Kumar

Reputation: 701

How to copy chart from excel and paste it as chart into powerpoint (not image) using python

I have an excel file which generates chart based on the data available, the chart name is thisChart.

I want to copy thisChart from excel file to the ppt file. Now I know the 2 ways to do that ie VBA and python(using win32com.client). The problem with VBA is that its really time consuming and it randomly crashes this needing constant supervision thus I planned to do the same using python.

After researching I found out about win32com.client in python which allowed me to do the same.

I used the following script to do so.


# Grab the Active Instance of Excel.
ExcelApp = win32com.client.GetActiveObject("Excel.Application")
ExcelApp.Visible = True

# Grab the workbook with the charts.
xlWorkbook = ExcelApp.Workbooks.Open(r'C:\Users\prashant.kumar\Desktop\testxl.xlsx')

# Create a new instance of PowerPoint and make sure it's visible.
PPTApp = win32com.client.gencache.EnsureDispatch("PowerPoint.Application")
PPTApp.Visible = True

# Add a presentation to the PowerPoint Application, returns a Presentation Object.
PPTPresentation = PPTApp.Presentations.Add()

# Loop through each Worksheet.
for xlWorksheet in xlWorkbook.Worksheets:

    # Grab the ChartObjects Collection for each sheet.
    xlCharts = xlWorksheet.ChartObjects()

    # Loop through each Chart in the ChartObjects Collection.
    for index, xlChart in enumerate(xlCharts):
        # Each chart needs to be on it's own slide, so at this point create a new slide.
        PPTSlide = PPTPresentation.Slides.Add(Index=index + 1, Layout=12)  # 12 is a blank layout

        # Display something to the user.
        print('Exporting Chart {} from Worksheet {}'.format(xlChart.Name, xlWorksheet.Name))

        # Copy the chart.
        xlChart.Copy()

        # Paste the Object to the Slide
        PPTSlide.Shapes.PasteSpecial(DataType=1)

    # Save the presentation.
PPTPresentation.SaveAs(r"C:\Users\prashant.kumar\Desktop\outppt")

but it pastes the chart as an image whereas I want the chart to be pasted as interactive chart (just how it is when in the excel file)

The reason is that the quality deteriorates and it does not give me much flexibility to add minor modifications to chart in the ppt in future when needed.

Here is a comparison of the 2 outputs

enter image description here

The quality difference can be seen here and it gets worse when I zoom in.

Now my question is, Is there any way to paste the chart from excel to ppt in the chart format using python or any other way which is faster than VBA?

PS. I don't want to read the excel file data and generate chart in python and then paste to PPT since the actual charts are really complicated and would probably be very hard to make

Upvotes: 4

Views: 7559

Answers (2)

Priyanka Srivastava
Priyanka Srivastava

Reputation: 21

Try PPTSlide.Shapes.Paste instead of PasteSpecial in your existing code.

This will copy and paste the chart in an editable format, not as a picture.

I struggled with it as well and found it to be useful.

Upvotes: 2

Pradeep Jassal
Pradeep Jassal

Reputation: 11

Please try PPTSlide.Shapes.PasteSpecial() instead of PPTSlide.Shapes.PasteSpecial(DataType=1) in your existing code. This will help to keep the source in you shapes which keeps it live and wont degrade the quality of image.

Upvotes: 1

Related Questions