Powerpoint VBA / Python:以编程方式将不同的字体大小应用于TextBox中的单词

时间:2013-09-12 15:17:40

标签: python powerpoint-vba words textrange

目标:以编程方式调整powerpoint中SINGLE文本框中单词组的字体大小。

详细说明:

我有两个清单:

Labels = ["Mahon Point Retail","Park","Blackpool Drive","Balance","Finglas Point"] 
FontSize = [10,23,15,20,40]

我想通过索引将FontSize中的字体大小应用于标签中的标签。

到目前为止我的脚本:

#add all items in Labels to a single textbox
for i, label in enumerate(labels):
     Shape.TextFrame.TextRange.Text += "  " + label 

#apply font size from FontSize list to its corresponding label
for x, num in enumerate(FontSize, 1):
     Shape.TextFrame.TextRange.Words(x).Font.Size = int(num)

问题:

我认为问题在于使用“Words(x)”属性,有什么方法可以定义一个单词是什么?因为现在它认为“Mahon Point零售”是3个单词(它是)但是我希望它把它看作一个单词......

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

无法帮助Python部分,但您可以调整此VBA以执行您想要的操作。首先是为任何子字符串设置所需格式的函数,然后是测试子例程。 这只会改变字符串中单词的第一个实例。从一个循环中重复调用它来测试更大字符串中是否存在字符串将解决这个问题。

Function FontTheWords(oSh As Shape, sWords As String)

    Dim oRng As TextRange
    ' Get a range object representing the chosen words
    Set oRng = oSh.TextFrame.TextRange.Characters(InStr(oSh.TextFrame.TextRange.Text, sWords), Len(sWords))
    Debug.Print oRng.Text
    ' format the range in whatever way you like
    With oRng.Font
        .Bold = True
        .Color.RGB = RGB(255, 0, 0)
    End With

End Function

Sub TestIt()
    FontTheWords ActiveWindow.Selection.ShapeRange(1), "Blackpool Drive"
End Sub