gpt4 book ai didi

python - 如何使用 Python 和 win32com.client 在 PowerPoint 中操作形状(颜色)?

转载 作者:行者123 更新时间:2023-11-28 17:12:20 25 4
gpt4 key购买 nike

自从我发布这个问题以来,我手头的问题取得了轻微的进展,我发现有必要将它分成两部分以保持清晰。


  1. 如何使用 Python 和 win32com.client 在 PowerPoint 中处理形状颜色?
  2. 如何使用 dir() 在 Python 中检查 com 对象?

1。使用 Python 在 PowerPoint 中操作形状颜色

有一些关于如何使用 pptx 库 编辑 PowerPoint 幻灯片的示例 here .但是,我发现使用 win32com.client 操作事件的 PowerPoint 演示文稿要容易得多,如 here 所述。 .使用 Microsoft Developer Network 中的示例我发现我可以轻松复制此 VBA 代码段的部分功能...

With ActivePresentation.Slides(1).Shapes(1)
With .TextFrame.TextRange.Font
.Size = 48
.Name = "Palatino"
.Bold = True
.Color.RGB = RGB(255, 127, 255)
End With
End With

...使用这个 Python 片段:

import win32com.client
Application = win32com.client.Dispatch("PowerPoint.Application")
Presentation = Application.Activepresentation

slidenr = Presentation.Slides.Count
slide = Presentation.slides(slidenr)

shape1 = slide.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=100,Width=100,Height=100)
shape1.TextFrame.TextRange.Text='Hello, world'

#Manipulate font size, name and boldness
shape1.TextFrame.TextRange.Font.Size=20
shape1.TextFrame.TextRange.Characters(1, 4).Font.Name = "Times New Roman"
shape1.TextFrame.TextRange.Font.Bold=True

在这里,我可以操纵字体大小和名称。我还可以通过更改来更改文本框的方向Orientation=0x1Orientation=0x5shape1 = slide.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=100,Width=100,Height =100)

但似乎不可能的是编辑框或字体颜色。

这不起作用:

shape1.TextFrame.TextRange.Font.Color.RGB = RGB(255, 127, 255)

错误信息:

enter image description here

我非常希望通过在 pypi.python.org 上的信息导入一些 RGB 功能来解决这个问题

但是我在这里也遇到了 pip install colour 的问题:

enter image description here

到现在为止,我对所有帐户都有点迷失,所以任何以任何方式操纵颜色的提示都会很棒!

2。使用 dir()

在 Python 中检查对象

在尝试管理那些讨厌的颜色时,我开始检查 dir(shape1.TextFrame)dir(shape1.TextFrame.Textrange) 等的输出.令我失望的是,我找不到任何关于颜色的信息,甚至找不到字体,尽管字体显然可以进行操作。

所以我的第二个问题是:这根本不是检查和操作这些形状的方法吗?我怎样才能找到正确的对象(或方法?)来进一步操纵 shape1?我看过 PowerPoint objectmodel ,但收效甚微。

感谢您的任何建议!

最佳答案

您可以在 python 脚本中轻松地重新创建全局 VBA 函数

def RGB(red, green, blue):
assert 0 <= red <=255
assert 0 <= green <=255
assert 0 <= blue <=255
return red + (green << 8) + (blue << 16)

关于您的第二个问题。了解这些对象的最佳位置是 Excel 宏对象浏览器。在宏编辑器中时,按 F2,然后筛选 Powerpoint 库。然后你可以搜索和探索与powerpoint相关的对象模型

Object Browser

关于python - 如何使用 Python 和 win32com.client 在 PowerPoint 中操作形状(颜色)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46789979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com