2012-08-16 7 views
5

xlsファイルで特定のセルと行をスタイル設定する必要がありますが、私は自分のプログラムで作成しますが、xlwt easyxfの仕組みに関するいくつかの問題が考えられます。Python xlwt:バグを書くときにeasyxfを使ってセルを整形する

最初に、値とスタイルだけを使わずにセルに書き込むと、内部の値は消去されますか?

第二に、私は、セルのスタイルと値を使用して、細胞への書き込みしようとしているが、私はエラーを取得しておいてください。

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

さて問題は、スタイルは実装されませんということです。セルを書き込んだ後、xlsファイルに出力すると、色、bg、サイズ、フォントの変更は一切ありませんでした。

私はこれを見つけようとしていて、他の人の例に従っていましたが、何らかの理由で私のコードは機能しません。ここでは、次のとおりです。

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。私はそれを複雑にすることやそれを非倫理的に行うことを余儀なくされるかもしれないが、それは機能する。

P.S.私はこれを行うか、または異なる方法でフルカラーになるように特定の細胞を変更することができる別の方法がある場合は、私に教えてください。もう一度、ありがとう。

ありがとうございました。皆さんにお伝えしたコードを修正しました。TypeErrorは終了しましたが、完了した後に作成して使用したスタイリングオプションはありませんでした。 xlsファイルはまだデフォルトの形式です。どうすればいいの?

答えて

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) 
+0

おかげで、しかし、何らかの理由で、私のスタイルのいずれも制定されていません。すべてが動作し、コンパイルされて実行されますが、xlsドキュメントを開くと、フォントや色の変更はありません。変更されるのは列の幅だけです。 –

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() 
関連する問題