34

xlrdを使用して Excel シートからデータを読み取る python スクリプトを作成しています。ワークシートのいくつかのセルが異なる色で強調表示されているため、セルのカラー コードを特定したいと考えています。それを行う方法はありますか?例をいただければ幸いです。

4

4 に答える 4

43

これを処理する 1 つの方法を次に示します。

import xlrd
book = xlrd.open_workbook("sample.xls", formatting_info=True)
sheets = book.sheet_names()
print "sheets are:", sheets
for index, sh in enumerate(sheets):
    sheet = book.sheet_by_index(index)
    print "Sheet:", sheet.name
    rows, cols = sheet.nrows, sheet.ncols
    print "Number of rows: %s   Number of cols: %s" % (rows, cols)
    for row in range(rows):
        for col in range(cols):
            print "row, col is:", row+1, col+1,
            thecell = sheet.cell(row, col)      
            # could get 'dump', 'value', 'xf_index'
            print thecell.value,
            xfx = sheet.cell_xf_index(row, col)
            xf = book.xf_list[xfx]
            bgx = xf.background.pattern_colour_index
            print bgx

Python-Excel Google Groupの詳細情報。

于 2011-11-03T07:29:06.587 に答える