Python XLWT 多种款式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18279785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:22:20  来源:igfitidea点击:

XLWT multiple styles

pythonexcelxlwt

提问by Jon Hagelin

This has been bogging my mind with my current project. I'm trying to write styles into an excel sheet using XLWT, see below:

这一直困扰着我当前的项目。我正在尝试使用 XLWT 将样式写入 excel 表,见下文:

sheet.write(rowi,coli,value,stylesheet.bold,stylesheet.bordered)

I'm running into this error:

我遇到了这个错误:

TypeError: write() takes at most 5 arguments (6 given)

类型错误:write() 最多需要 5 个参数(给出 6 个)

Any idea how to get around this to add multiple styles to a certain cell? Is it possible to do a list here?

知道如何解决这个问题以向某个单元格添加多种样式吗?可以在这里做一个清单吗?

采纳答案by alecxe

You should pass only row number, col number, value and style (XFStyleclass instance) to the writemethod, for example:

您应该只将行号、列号、值和样式(XFStyle类实例)传递给该write方法,例如:

import xlwt

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Test')

style = xlwt.XFStyle()

# font
font = xlwt.Font()
font.bold = True
style.font = font

# borders
borders = xlwt.Borders()
borders.bottom = xlwt.Borders.DASHED
style.borders = borders

worksheet.write(0, 0, 'test value', style=style)
workbook.save('test.xls')

The same thing, but using easyxf:

同样的事情,但使用easyxf

import xlwt

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Test')  

style_string = "font: bold on; borders: bottom dashed"
style = xlwt.easyxf(style_string)

worksheet.write(0, 0, 'test value', style=style)
workbook.save('test.xls')