Python 对文本框中的单词应用不同的字体大小
目的:在PowerPoint中调整单个文本框中一组单词的字体大小 详情: 我有两份清单:Python 对文本框中的单词应用不同的字体大小,python,vba,powerpoint,textrange,Python,Vba,Powerpoint,Textrange,目的:在PowerPoint中调整单个文本框中一组单词的字体大小 详情: 我有两份清单: Labels = ["Mahon Point Retail","Park","Blackpool Drive","Balance","Finglas Point"] FontSize = [10,23,15,20,40] 我想通过索引将FontSize中的字体大小应用于labels中的标签 我的剧本: #
Labels = ["Mahon Point Retail","Park","Blackpool Drive","Balance","Finglas Point"]
FontSize = [10,23,15,20,40]
我想通过索引将FontSize中的字体大小应用于labels中的标签
我的剧本:
#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)
问题是:
我相信问题在于“单词(x)”属性的使用,有没有办法定义单词是什么?它将“Mahon Point Retail”视为三个单词,但我想将其视为一个单词。对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