1

Given below is the code I'm running. What I'm trying to achieve is simple. I need to copy a chart from an existing Excel file & paste as a chart (NOT image) into an existing PowerPoint slide.

But I always get the error

"AttributeError: 'Sheet' object has no attribute 'Shapes'"

Is there any package I'm supposed to install?

from pptx import Presentation
import xlrd
import math
import xlutils

import win32com.client
from win32com.client import constants

prs = Presentation('D:\Sruti K\Online_Retail\APAC\Office Slides\Office QBU Slides\FY17 Q4 June\Office Slides FY17 Q4 test.pptx')
title_slide_layout = prs.slide_layouts[3]
slide = prs.slides.add_slide(title_slide_layout)

book = xlrd.open_workbook(r'D:\Sruti K\Online_Retail\APAC\Office Slides\Office QBU Slides\FY17 Q4 June\Python Chart Paste.xlsx')
ws = book.sheet_by_index(0)

sheetsinbook=[book.sheet_by_index(0)]


for ws in sheetsinbook:
    for chart in ws.Shapes():
        chart.Activate()
        chart.Copy()
        slide.Shapes.PasteSpecial(constants.ppPasteShape)
        Print("hi")

prs.save(r'D:\Sruti K\Online_Retail\APAC\Office Slides\Office QBU Slides\FY17 Q4 June\Python Trial APAC PPT V1.pptx')
stovfl
  • 14,998
  • 7
  • 24
  • 51

2 Answers2

0

The problem is you are capitalizing random words, which changes what they do/call. For example you typed Print instead of print which is not valid unless you have a class named Print. If you use ws.shapes() instead it should work (and un-capitalize some of those other words too).

Cary Shindell
  • 1,336
  • 8
  • 25
  • Hi Cary! I have done this but I'm still getting the same error. I'm using the 2.7.13 version. Could it be because of this? I have also tried the code posted here : https://stackoverflow.com/questions/32639900/charts-from-excel-to-powerpoint-with-python but this throws up a similar error as well. – Sruti Kulanthaivel Jul 23 '17 at 19:19
  • Did you try `for chart in ws.ChartObjects`? – Cary Shindell Jul 24 '17 at 13:11
  • Hey Cary! AttributeError: 'Sheet' object has no attribute 'ChartObjects' is what I'm seeing here. :( – Sruti Kulanthaivel Jul 25 '17 at 09:48
0

The problem is you're calling for COM API inside python-pptx which does not have that.

What you need to do is use win32com.client instead of python-pptx, you can refer the documentation (but this one is for VB, but it got almost the same implementation for python) and here's for your PasteSpacial

I use Paste instead of PasteSpecial for the example:

import win32com.client
import pyperclip

Application = win32com.client.Dispatch("PowerPoint.Application")
Presentation = Application.Presentations.Add()
Slide = Presentation.Slides.Add(1, 12)
pyperclip.copy('Some text.')
Slide.Shapes.Paste()

Hope this helps.

Zigatronz
  • 134
  • 1
  • 8