1

我正在尝试使用 python 的 xlwt 包将我的 excel 电子表格的颜色更改为颜色代码数据。这是我当前的代码:

from xlrd import open_workbook
from tempfile import Temporary File
from xlwt
import sys
import csv

....
....#Some other code

style = xlwt.XFStyle() #this is the line throwing the error
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = xlwt.Style.colour_map['red']

pattern.pattern_fore_colour = xlwt.Style.colour_map['red']
newSheet.write(row, col, values[row - 1][col], pattern)

....
....#Some other code

其他代码部分在那里,因为它的脚本比显示的要长,但这些代码部分与此问题无关

我在运行时得到以下堆栈跟踪:

Traceback (most recent call last):
   File "excel-procesor.py", line 85, in <module>
        newSheet.write(row, col, values[row - 1][col], pattern)
   File "/usr/local/lib/python2.7/dist-packages/xlwt/Worksheet.py", line 1030, in write self.row(r).write(c, label, style)
   File "/usr/local/lib/python2.7/dist-packages/xlwt/Row.py", line 234, in write self.__adjust_height(style)
   File "/usr/local/lib/python2.7/dist-packages/xlwt/Row.py", line 64, in __adjust_height
        twip = style.font.height
AttributeError: 'Pattern' object has no attribute 'font'

我正在使用如何使用 python xlwt 库更改 excel 单元格的背景颜色?python xlwt 设置单元格的自定义背景颜色作为指南。

4

1 回答 1

1

根据源代码:

xlwt.Worksheet.write(self, r, c, label='', style=<xlwt.Style.XFStyle object at 0x053AEA10>)

write应该接收styleas 参数,而不是直接接收Pattern对象。

您应该尝试将您的模式对象放入并作为参数style传递:style

style.pattern = pattern
newSheet.write(row, col, values[row - 1][col], style)

Some examples I found useful how to use xlwt, including how to set the style

于 2013-05-31T16:46:17.627 回答