如何让xlsx文件的边框使用xlrd /

时间:2014-07-29 05:01:06

标签: python excel xlrd

我想获得一个xlsx文件的边框使用xlrd,例如:

book = xlrd.open_workbook("./file/test.xls", formatting_info=True)
sheet = book.sheet_by_index(0)
cell = Cell(sheet)
nrows = sheet.nrows
ncols = sheet.ncols
row_list = []
for row in range(nrows):
    col_list = []
    for col in range(ncols):
        fmt = book.xf_list[sheet.cell(row, col).xf_index]
        col_list.append(fmt.border)
    row_list.append(col_list)
merged_cells = []
print row_list[5][1].dump()

控制台中的信息:

bottom_colour_index: 63
bottom_line_style: 2
diag_colour_index: 0
diag_down: 0
diag_line_style: 0
diag_up: 0
left_colour_index: 63
left_line_style: 2
right_colour_index: 8
right_line_style: 2
top_colour_index: 63
top_line_style: 2
None

如果我打开像这些代码的xlsx文件:

book = xlrd.open_workbook("./file/test.xlsx", formatting_info=False)
sheet = book.sheet_by_index(0)
cell = Cell(sheet)
nrows = sheet.nrows
ncols = sheet.ncols
row_list = []
for row in range(nrows):
    col_list = []
    for col in range(ncols):
        fmt = book.xf_list[sheet.cell(row, col).xf_index]
        col_list.append(fmt.border)
    row_list.append(col_list)
merged_cells = []
print row_list[5][1].dump()

控制台中的信息如下:

  

回溯(最近一次呼叫最后):文件" judge_merged.py",第68行,   在       fmt = book.xf_list [sheet.cell(row,col).xf_index] TypeError:list indices必须是整数,而不是NoneType

我不知道如何解决这个问题。我需要帮助。

1 个答案:

答案 0 :(得分:2)

由于您传递formatting_info=False,所有单元格的xf_index将为None

使用formatting_info=True打开工作簿。