【问题标题】:python-pptx duplicate slide PPT will be damagedpython-pptx复制幻灯片PPT会损坏
【发布时间】:2021-03-07 05:50:52
【问题描述】:

我发现在使用复制幻灯片的方法时,如果页面上有图表,PPT会损坏,所以我用这种方法复制了一张带有图表的幻灯片,并修改了一页图表的标题,而另外一页的图表标题也被莫名修改了

def duplicate_slide(pres,index):

    template = pres.slides[index]
    blank_slide_layout = pres.slide_layouts[index]
    copied_slide = pres.slides.add_slide(blank_slide_layout)

    for shp in template.shapes:
        el = shp.element
        newel = copy.deepcopy(el)
        copied_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')

    for _, value in six.iteritems(template.part.rels):
        # Make sure we don't copy a notesSlide relation as that won't exist
        if "notesSlide" not in value.reltype:
            copied_slide.part.rels.add_relationship(
                value.reltype, value._target, value.rId
            )

    return copied_slide

【问题讨论】:

    标签: python python-pptx


    【解决方案1】:

    在一般情况下,复制幻灯片并不像克隆幻灯片 XML 那样简单,这正是您的 duplicate_slide() 方法所做的。这适用于一些简单的情况,但不适用于带有图表的幻灯片。

    特别是,图表是 PPTX 包(zip 存档)中的一个单独的包部分(“文件”)。如果您只是将关系从一张幻灯片复制到另一张幻灯片,就像您在此处所做的那样,那么您将有两张幻灯片指向同一个图表部分。这就是为什么更改一张幻灯片中的图表标题也会改变另一张幻灯片的原因,因为两张幻灯片上都显示了同一个图表。

    为了获得您似乎正在寻找的行为,您还需要复制图表部分并形成从新幻灯片到该图表部分的关系。

    这不是一个简单的过程,我在这里只提供几行代码来完成它,但希望这可以解释为什么你会看到你的行为。

    【讨论】:

    • 非常感谢scanny,有成熟的方法可以复制一整页PPT吗?
    猜你喜欢
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2011-02-27
    相关资源
    最近更新 更多