gpt4 book ai didi

python - 如何使用 Python 仅将 Excel 单元格中字符串的一部分加粗?

转载 作者:行者123 更新时间:2023-12-02 07:45:47 27 4
gpt4 key购买 nike

我正在使用 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

选择对象具有“字符”属性,但我找不到任何有关其用途的文档。

最佳答案

@ron-rosenfield ,实现此目的的 VBA 代码如下所示:

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

同样,Excel WorkSheet的Range有一个Characters对象属性

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

但是,使用该对象访问范围所需的方法是 GetCharacters(start, length)(索引从 1 开始,与 excel com 方法一样)

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

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

关于python - 如何使用 Python 仅将 Excel 单元格中字符串的一部分加粗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26489355/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com