如何粗体显示Excel单元格中的一部分string与Python

我正在使用win32com来填写Excel电子表格和一些分析信息。 我有一个单元格,我想要在这种forms:

这是问题描述: 这是您运行修复它的命令

我似乎无法弄清楚如何将文本与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 

select对象具有“字符”属性,但我无法find任何文档。 我在这里有一点点损失,我可以使用一个帮助。

谢谢

我终于find了,在这里张贴,所以我们的堆栈溢出霸主也可以有答案。

Per @ ron-rosenfield,VBA代码将这个closures如下:

 Range("A1").Characters(2,5).Font.Bold = true 

同样,还有一个Excel工作表范围的字符对象属性

 >>> ws.Range("A1").Characters <win32com.gen_py.Microsoft Excel 14.0 Object Library.Characters 967192> 

然而,你需要访问该对象的范围的方法是GetCharacters(开始,长度)(索引从1开始与Excel通常的方法)

 ws.Range("A1").GetCharacters(2,4).Font.Bold = True 

事实之后,您可以使用此命令更新单元格中的字符的粗体。

虽然你正在寻找一个win32com解决scheme在你的问题的主体,我会指出任何人通过标题来到这里,这也可能在Python与XlsxWriter。

例如 :

在这里输入图像说明