PowerPoint &Python:到达图像时编辑PPT失败 [英] PowerPoint & Python: Editing PPT fails when an image is reached

查看:59
本文介绍了PowerPoint &Python:到达图像时编辑PPT失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在整理一个 Python 脚本,该脚本将清理"PowerPoint 文件的字体、字体颜色、字体大小等.我发现了一段代码可以完成我需要它做的事情,但是它似乎在遇到幻灯片中的图像时就会破裂.

I am putting together a python script that will "clean up" a PowerPoint file's font-face, font-colors, font-size, etc. I found a snippet of code that does what I need it to do, however it seems to break as soon as it meets an image in a slide.

    import win32com.client, sys
Application = win32com.client.Dispatch("PowerPoint.Application")
Application.Visible = True
Presentation = Application.Presentations.Open(sys.argv[1])
for Slide in Presentation.Slides:
    for Shape in Slide.Shapes:
        Shape.TextFrame.TextRange.Font.Name = "Arial"
        Shape.TextFrame.TextRange.Font.Size = "12"
        Shape.TextFrame.TextRange.Font.Color.RGB = "000000"
Presentation.Save()
Application.Quit()

复制并粘贴错误的代码...删除了无效的 if 语句.

Copy and pasted the wrong code... removed non-working if statement.

这一切都运行得很好,清理看起来很傻的字体和颜色,直到它到达第一个图像.然后它打破了,我看到了这个:

This all runs just fine, cleaning up silly looking fonts and colors until it hits its first image. Then it breaks and I am presented with this:

Traceback (most recent call last):
  File "c:/pptpy/convert.py", line 7, in <module>
    Shape.TextFrame.TextRange.Font.Name = "Arial"
  File "C:\Python33\lib\site-packages\win32com\client\dynamic.py", line 511, in __getattr__
    ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, 'The specified value is out of range.', None, 0, -2147024809), None)

如果我从文件中删除所有图像(不是形状)并运行脚本,它会很好地工作并且我留下了一个不错的 powerpoint.然而,图像非常重要.

If I remove all images (not shapes) from the file and run the script, it works great and I'm left with a decent powerpoint. The images are pretty important, however.

详情:

Python 3.3

PowerPoint 2007

PowerPoint 2007

一旦完成,有望一次批量转换 200-300 个 PPT(x) 的脚本.

Script to hopefully convert batches of 200-300 PPT(x) at a time, once finished.

如果您需要更多详细信息,请告诉我!谢谢!

If you need any more details, let me know! Thanks!

推荐答案

虽然您的代码实际上不起作用,但用 try catch 替换您的 if 工作正常.这可能不是一种优雅的方法.

While your code is not actually working replacing your if with a try catch works fine. This may not be a elegant method tough.

import win32com.client, sys
Application = win32com.client.Dispatch("PowerPoint.Application")
Application.Visible = True
Presentation = Application.Presentations.Open(sys.argv[1])
for Slide in Presentation.Slides:
    for Shape in Slide.Shapes:
        try:
            Shape.TextFrame.TextRange.Font.Name = "Arial"
            Shape.TextFrame.TextRange.Font.Size = "12"
            Shape.TextFrame.TextRange.Font.Color.RGB = "000000"
        except:
            pass
Presentation.Save()
Application.Quit()  

您可能会遇到的情况是 if 始终为真,因为一个对象有资格为真.尝试询问大于 0 的对象的长度或类似的东西.

What probably happens to you is that the if is always true since an object qualifies as true. Try asking length of the object greater than 0 or something similar.

这篇关于PowerPoint &amp;Python:到达图像时编辑PPT失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆