如何使用python win32com更改excel中单元格中单个字符的颜色?

chr*_*ian 1 python excel fonts win32com

我有关于excel的win32com绑定的问题.我设置了早期绑定,并遵循了O'Reilly的"Python编程在Win32上"一书中的一些例子.

以下代码工作正常:

book2.xlApp.Worksheets('Sheet1').Cells(1,1).Font.ColorIndex = 1
book2.xlApp.Worksheets('Sheet1').Cells(1,1).Font.ColorIndex = 2
Run Code Online (Sandbox Code Playgroud)

它根据数字改变整个单元格的字体颜色.但是这不起作用:

book2.xlApp.Worksheets('Sheet1').Cells(1,1).Characters(start,length).Font.ColorIndex = 1
Run Code Online (Sandbox Code Playgroud)

我得到以下回调:

Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: Characters instance has no __call__ method
Run Code Online (Sandbox Code Playgroud)

但是在Excels VBA中,代码可以正常工作.任何人都能指出我的解决方案吗?我真的需要在excel单元格中更改字符串的一部分.

非常感谢你.

HYR*_*YRY 5

使用GetCharacters:

Cells(1,1).GetCharacters(start,length).Font.ColorIndex = 1
Run Code Online (Sandbox Code Playgroud)