粘贴Powerpoint时保留原始形状属性

时间:2019-11-21 01:32:10

标签: python-3.x powerpoint win32com

在python中使用win32com.client。

我具有从excel文件复制表格并将其粘贴到幻灯片中的占位符对象的功能。

import win32com.client

def copy_table_to_placeholder(table_placeholer_obj,excel_file):
    """
        Copies table from first sheet of excel file to table placeholder in powerpoint. 
        Powerpoint object is required for Paste With Source Formatting option. For finding
        table range, it is assumed table starts at cell A1.
    """
    # start excel
    excel_obj    = win32com.client.Dispatch("Excel.Application")
    # open the workbook
    workbook_obj = excel_obj.Worksheets.Open(excel_file)
    # select the sheet...use integer selection...
    sheet_obj    = workbook_obj.Worksheets.Item(worksheet)
    # select the table 
    range_obj    = sheet_obj.Range("A1").CurrentRegion
    # copy the table
    range_obj.Copy()
    # now paste it to the placeholder
    table_placeholer_obj.Application.CommandBars.ExecuteMso("PasteSourceFormatting")
    # pasting the table will cause a new shape to be named and original to be removed.
    # I need to find a way to hold the placeholder size,pos,name
    return

我的问题是,当我执行最后一行时,它将粘贴一个具有新名称的全新形状。我使用COM中的.item()方法按名称引用所有这些项目,并且名称更改的事实确实使编写代码变得困难。

粘贴之前的示例

The table is called "Table Placeholder 4"

现在粘贴之后

New table is called "Table Placeholder 23"

0 个答案:

没有答案