简体   繁体   English

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

[英]How to apply bold style to a specific word in Excel file using Python?

I am using pyexcelerator Python module to generate Excel files.我正在使用pyexcelerator Python 模块来生成 Excel 文件。 I want to apply bold style to part of cell text, but not to the whole cell.我想将粗体样式应用于部分单元格文本,而不是整个单元格。 How to do it?怎么做?

Found example here: Generate an Excel Formatted File Right in Python在这里找到示例: 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)

This is an example from Excel documentation:这是 Excel 文档中的一个示例:

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.因此,您要操作的单元格的 Characters 属性就是您问题的答案。 It's used as Characters( start , length ).它用作 Characters( start , length )。

PS: I've never used the module in question, but I've used Excel COM automation in python scripts. PS:我从未使用过有问题的模块,但我在 python 脚本中使用了 Excel COM 自动化。 The Characters property is available using win32com.使用 win32com 可以使用 Characters 属性。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在excel中创建超链接以通过python在特定页面中打开word文件 - How to create a hyperlink in excel to open word file in specific page by python Python:在Excel文件中查找特定的单词或值 - Python: Looking for specific word or values in an excel file 无法使用 docx python 在 word doc 中应用表格样式 - Cant apply table style in word doc using docx python 如何使用Python从Excel文件中提取单元格格式(粗体,斜体等)? - How to extract cell format (bold, italic, …) from an Excel file using Python? 有什么方法可以使用openpyxl python从excel文件中读取单元格内每个单词的样式 - Is there any way to while reading from excel file get style of each word inside cell using openpyxl python 如何使用XLWT Python Excel将样式应用于整个行? - How do I apply a style to the whole row using XLWT Python Excel? 如何使用python查找文本文件中的特定单词 - How to find specific word in text file using python 如何使用python替换文本文件中某些特定单词的字符 - How to replace a character in some specific word in a text file using python 如何使用 python 从 csv 文件调用特定单词 - How to call a specific word from csv file using python 使用 python 的 excel 文件中的一行字数 - Word count of a row in excel file using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM