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_sheetxlwt ワークブックに追加したシートを表す別の関数で作成したグローバル変数です。私が渡すワークブックは、それを含むはずのファイルです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 に答える