Der*_*ell 5 python excel formatting win32com
我正在使用win32com填写一些分析信息的Excel电子表格。我有一个要设置为这种形式的单元格:
这是问题描述:这是您运行以修复它的命令
我似乎无法弄清楚如何使用混合格式的python将文本放入单元格中。
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
ws = wb.Worksheets("Sheet1")
excel.Visible=True
sel = excel.Selection
sel.Value = "this is the command you run to fix it"
sel.Font.Bold = True
ws.Cells(1,1).Value = 'This is the problem description' + sel.Value #makes the whole cell bold
ws.Cells(1,1).Value = "{} {}".format("this is the problem desc",sel) #makes the whole cell bold
Run Code Online (Sandbox Code Playgroud)
选择对象具有Characters属性,但是我找不到有关其功能的任何文档。我在这里有点茫然,我可以帮忙。
谢谢
我终于找到了,在这里发布,这样我们的堆栈溢出霸主也可以得到答案。
根据@ ron-rosenfield,实现此目的的VBA代码如下所示:
Range("A1").Characters(2,5).Font.Bold = true
Run Code Online (Sandbox Code Playgroud)
同样,excel WorkSheet的Range有一个Characters对象属性
>>> ws.Range("A1").Characters
<win32com.gen_py.Microsoft Excel 14.0 Object Library.Characters 967192>
Run Code Online (Sandbox Code Playgroud)
但是,您需要使用该对象访问范围的方法是GetCharacters(start,length)(与excel com方法一样,索引通常从1开始)
ws.Range("A1").GetCharacters(2,4).Font.Bold = True
Run Code Online (Sandbox Code Playgroud)
您可以使用此命令在事实之后更新单元格中字符的粗体。