python win32com excel中如何改变单元格中单个字符的颜色?
我有一个关于excel的win32com绑定的问题。 我设置了早期的绑定,并遵循O'Reilly的“Win32的Python编程”一书中的一些例子。
下面的代码工作正常:
book2.xlApp.Worksheets('Sheet1').Cells(1,1).Font.ColorIndex = 1 book2.xlApp.Worksheets('Sheet1').Cells(1,1).Font.ColorIndex = 2
它根据编号改变整个单元的字体颜色。 但是这不起作用:
book2.xlApp.Worksheets('Sheet1').Cells(1,1).Characters(start,length).Font.ColorIndex = 1
我得到以下callback:
Traceback (most recent call last): File "<interactive input>", line 1, in <module> AttributeError: Characters instance has no __call__ method
但在Excels VBA中,代码起作用。 任何人都可以指出我的解决scheme? 我真的需要在Excel单元格中更改一部分string。
非常感谢你。
使用GetCharacters:
Cells(1,1).GetCharacters(start,length).Font.ColorIndex = 1