简体   繁体   English

Python xlsxwriter,在我编写单元格而不是批量时将格式添加到单元格

[英]Python xlsxwriter, add formatting to cell as I write them instead of bulk

Is it possible to apply formatting conditions as I write the data to the worksheet instead of doing this: 当我将数据写入工作表时,是否可以应用格式化条件而不是这样做:

worksheet.conditional_format('B3:K12', {'type': 'duplicate',
                                     'format': format})

So something like: 所以像这样:

worksheet.write(CELL, "yada yada yada", format=format)

Yes, I think this is possible. 是的,我认为这是可能的。

You can start with a base format: 您可以从基本格式开始:

base = {
    'num_format':'#,##0', 
    'font_size':'12'
}

As you write you can use the format.set_ methods to change or add elements to your base format as you write data. 在编写数据时,可以使用format.set_方法在写入数据时更改或向基本格式添加元素。

I typically write in a loop like fashion and change the formatting based on the columns. 我通常以类似循环的方式编写代码,并根据列更改格式。

Example: 例:

body = workbook.add_format(base)

body.set_num_format(some_format) # number example

body.set_font_name(some_format) # font example

sheet.write(CELL, some data, body)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM