简体   繁体   English

AttributeError: 'Sheet' object 没有属性 'Shapes'

[英]AttributeError: 'Sheet' object has no attribute 'Shapes'

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.我需要从现有的 Excel 文件中复制图表并将其作为图表(非图像)粘贴到现有的 PowerPoint 幻灯片中。

But I always get the error但我总是得到错误

"AttributeError: 'Sheet' object has no attribute 'Shapes'" “AttributeError:'Sheet' object 没有属性 'Shapes'”

Is there any package I'm supposed to install?我应该安装任何 package 吗?

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')

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. 例如,您键入Print而不是print ,除非您有一个名为Print的类,否则它是无效的。 If you use ws.shapes() instead it should work (and un-capitalize some of those other words too). 如果您使用ws.shapes()代替,它应该可以工作(也可以取消对其中一些单词的大写)。

The problem is you're calling for COM API inside python-pptx which does not have that.问题是你在python-pptx中调用 COM API ,它没有。

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您需要做的是使用win32com.client而不是python-pptx ,您可以参考文档(但这个是针对 VB 的,但它对 python 的实现几乎相同), 这里是您的PasteSpacial

I use Paste instead of PasteSpecial for the example:例如,我使用Paste而不是PasteSpecial

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.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM