5

我需要对通过程序创建的 xls 文件中的某些单元格和行进行样式化,但是我遇到了一些问题,可能对 xlwt easyxf 的工作方式有误解。

首先,如果我在没有值和样式的情况下写入单元格,里面的值会被删除吗?

其次,我正在尝试使用单元格的样式和值写入单元格,但我不断收到错误消息:

"TypeError: 'XFStyle' object is not callable". -Solved

现在的问题是样式没有得到实现。写入单元格并将其输出到 xls 文件后,颜色、背景、大小、字体完全没有变化。

我尝试使用谷歌搜索并遵循其他人的示例,但无论出于何种原因,我的代码都不起作用。这里是:

def stylize_spreadsheet_accordingly(iFile, workbook, row_grey, row_underline, row_font_size, cells_yellow):
    #this time iFile is the file you're overwriting

    #styling stuff

    print "styling the document..."

    new_sheet.col(0).width = 256 * 18
    new_sheet.col(1).width = 256 * 69.43
    new_sheet.col(2).width = 256 * 9
    new_sheet.col(3).width = 256 * 20.71
    new_sheet.col(4).width = 256 * 8.43

    font_size_style = xlwt.easyxf('font: name Calibri, bold on, height 280;')
    font_underline_style = xlwt.easyxf('font: underline on;')
    fill_grey_style = xlwt.easyxf('pattern: back_color gray25;')
    fill_yellow_style = xlwt.easyxf('pattern: back_color yellow;')

    iBook = open_workbook(iFile)
    iSheet = iBook.sheet_by_index(0)

    for row_index in range(iSheet.nrows):
        if row_index in row_grey:
            for col_index in range(iSheet.ncols):
                new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style)
        if row_index in row_underline:
            for col_index in range(iSheet.ncols):
                new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, font_underline_style)
        if row_index in row_font_size:
            for col_index in range(iSheet.ncols):
                new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, font_size_style)
    for each in cells_yellow:
        new_sheet.write(each[0], each[1], iSheet.cell(each[0],each[1]).value, fill_yellow_style)

    return workbook

new_sheet是我在另一个函数中创建的全局变量,它代表我添加到 xlwt 工作簿中的工作表。我传入的工作簿是应该包含new_sheet. 我可能过于复杂或不道德地这样做,但它确实有效。

PS如果有不同的方式我可以做到这一点或以不同的方式将某些单元格更改为全彩色,请告诉我。再一次,谢谢。

谢谢,我将代码修复为你们所说的,TypeError 消失了,但完成后,我创建和使用的样式选项都没有通过。xls 文件仍是其默认格式。怎么会这样?

4

3 回答 3

2

你得到'XFStyle' object is not callable是因为你像一个函数一样调用它,而不是仅仅将它传递给sheet.write例如

代替

new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style())

采用

new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style)
于 2012-08-16T22:17:00.027 回答
0

您正在创建样式,然后尝试调用它们。

例如:

font_size_style = xlwt.easyxf('font: name Calibri, bold on, height 280;')
...
new_sheet.write(row_index,col_index, iSheet.cell(row_index,col_index).value, fill_grey_style())

注意这一点:

fill_grey_style()
于 2012-08-16T22:17:34.190 回答
0

以下代码段可能会对您有所帮助,我使用此代码用灰色填充列

dep_style = xlwt.easyxf('font: bold on,height 200;''align: horiz left;''borders: left 
thin, right thin, top thin, bottom thin')

import xlwt
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = xlwt.Style.colour_map['gray25']
dep_style.pattern = pattern
sheet1.write_merge(s2,s2,0,int(count[0])+13,dep_data['department_name'],dep_style)

上面的代码给了我灰色的行,并附有名称。您可以根据需要更改此代码

于 2020-06-04T08:05:39.730 回答