如何使用Python将粗体样式应用于Excel文件中的特定单词?

发布于 2024-07-05 07:33:23 字数 84 浏览 6 评论 0原文

我正在使用 pyexcelerator Python 模块生成 Excel 文件。 我想将粗体样式应用于部分单元格文本,而不是整个单元格。 怎么做?

I am using pyexcelerator Python module to generate Excel files.
I want to apply bold style to part of cell text, but not to the whole cell.
How to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪我入戏太深 2024-07-12 07:33:23

在这里找到示例:生成 Excel 格式文件就在 Python 中

请注意,您创建了一个字体对象,然后将其赋予样式对象,然后在写入工作表时提供该样式对象:

import pyExcelerator as xl

def save_in_excel(headers,values):
    #Open new workbook
    mydoc=xl.Workbook()
    #Add a worksheet
    mysheet=mydoc.add_sheet("test")
    #write headers
    header_font=xl.Font() #make a font object
    header_font.bold=True
    header_font.underline=True
    #font needs to be style actually
    header_style = xl.XFStyle(); header_style.font = header_font
    for col,value in enumerate(headers):
        mysheet.write(0,col,value,header_style)
    #write values and highlight those that match my criteria
    highlighted_row_font=xl.Font() #no real highlighting available?
    highlighted_row_font.bold=True
    highlighted_row_font.colour_index=2 #2 is red,
    highlighted_row_style = xl.XFStyle(); highlighted_row_style.font = highlighted_row_font
    for row_num,row_values in enumerate(values):
        row_num+=1 #start at row 1
        if row_values[1]=='Manatee':
            for col,value in enumerate(row_values):
                #make Manatee's (sp) red
                mysheet.write(row_num,col,value,highlighted_row_style)
        else:
            for col,value in enumerate(row_values):
                #normal row
                mysheet.write(row_num,col,value)
    #save file
    mydoc.save(r'C:testpyexel.xlt')

headers=['Date','Name','Localatity']
data=[
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Manatee','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Manatee','San Jose'],
]

save_in_excel(headers,data)

Found example here: Generate an Excel Formatted File Right in Python

Notice that you make a font object and then give it to a style object, and then provide that style object when writing to the sheet:

import pyExcelerator as xl

def save_in_excel(headers,values):
    #Open new workbook
    mydoc=xl.Workbook()
    #Add a worksheet
    mysheet=mydoc.add_sheet("test")
    #write headers
    header_font=xl.Font() #make a font object
    header_font.bold=True
    header_font.underline=True
    #font needs to be style actually
    header_style = xl.XFStyle(); header_style.font = header_font
    for col,value in enumerate(headers):
        mysheet.write(0,col,value,header_style)
    #write values and highlight those that match my criteria
    highlighted_row_font=xl.Font() #no real highlighting available?
    highlighted_row_font.bold=True
    highlighted_row_font.colour_index=2 #2 is red,
    highlighted_row_style = xl.XFStyle(); highlighted_row_style.font = highlighted_row_font
    for row_num,row_values in enumerate(values):
        row_num+=1 #start at row 1
        if row_values[1]=='Manatee':
            for col,value in enumerate(row_values):
                #make Manatee's (sp) red
                mysheet.write(row_num,col,value,highlighted_row_style)
        else:
            for col,value in enumerate(row_values):
                #normal row
                mysheet.write(row_num,col,value)
    #save file
    mydoc.save(r'C:testpyexel.xlt')

headers=['Date','Name','Localatity']
data=[
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Manatee','San Jose'],
['June 11, 2006','Greg','San Jose'],
['June 11, 2006','Manatee','San Jose'],
]

save_in_excel(headers,data)
醉生梦死 2024-07-12 07:33:23

这是 Excel 文档中的一个示例:

With Worksheets("Sheet1").Range("B1")
    .Value = "New Title"
    .Characters(5, 5).Font.Bold = True
End With

因此,您要操作的单元格的 Characters 属性就是您问题的答案。 它用作字符(开始长度)。

PS:我从未使用过该模块,但我在 python 脚本中使用过 Excel COM 自动化。 可使用 win32com 来使用 Characters 属性。

This is an example from Excel documentation:

With Worksheets("Sheet1").Range("B1")
    .Value = "New Title"
    .Characters(5, 5).Font.Bold = True
End With

So the Characters property of the cell you want to manipulate is the answer to your question. It's used as Characters(start, length).

PS: I've never used the module in question, but I've used Excel COM automation in python scripts. The Characters property is available using win32com.

美男兮 2024-07-12 07:33:23

这是我用于解决同一问题的一种解决方案。

    import xlsxwriter
    workbook = xlsxwriter.Workbook(r'C:\workspace\NMSAutomation_001\FMGGUIAutomation\Libraries\Frontend\new_STICKERS_Final.xlsx')
####### two different formats
    bold   = workbook.add_format({'font_name':'Tahoma', 'bold': True, 'font_size':14})
    normal = workbook.add_format({'font_name':'Tahoma', 'font_size':11})

######## value is my string, bold and normal are my two different formats
    segments = [bold, value[:9], normal, value[9:]]
    worksheet.write_rich_string('A1', *segments) # 'A1' is cell position
    workbook.close()

Here is one solution which i had used for the same problem.

    import xlsxwriter
    workbook = xlsxwriter.Workbook(r'C:\workspace\NMSAutomation_001\FMGGUIAutomation\Libraries\Frontend\new_STICKERS_Final.xlsx')
####### two different formats
    bold   = workbook.add_format({'font_name':'Tahoma', 'bold': True, 'font_size':14})
    normal = workbook.add_format({'font_name':'Tahoma', 'font_size':11})

######## value is my string, bold and normal are my two different formats
    segments = [bold, value[:9], normal, value[9:]]
    worksheet.write_rich_string('A1', *segments) # 'A1' is cell position
    workbook.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文