首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python将粗体样式应用于Excel文件中的特定word?

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

Stack Overflow用户
提问于 2008-09-20 13:20:51
回答 3查看 7K关注 0票数 4

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

EN

回答 3

Stack Overflow用户

发布于 2008-09-20 13:54:05

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

代码语言:javascript
复制
With Worksheets("Sheet1").Range("B1")
    .Value = "New Title"
    .Characters(5, 5).Font.Bold = True
End With

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

PS:我从未使用过有问题的模块,但我在python脚本中使用了Excel COM自动化。使用win32com可以使用Characters属性。

票数 2
EN

Stack Overflow用户

发布于 2008-09-20 23:10:14

在这里找到了示例:Generate an Excel Formatted File Right in Python

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

代码语言:javascript
复制
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)
票数 2
EN

Stack Overflow用户

发布于 2021-02-18 00:36:40

这是我用来解决同样问题的一个解决方案。

代码语言:javascript
复制
    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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/108134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档