如何获得xlsx文件的边框使用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() 

控制台中的信息如下所示:

Traceback(最近调用最后一个):在fmt = book.xf_list [sheet.cell(row,col).xf_index]中的第68行的文件“judge_merged.py”TypeError:列表索引必须是整数,而不是NoneType

我不知道如何解决这个错误。 我需要帮助。

既然你传递的是formatting_info=False ,所有单元格的xf_index就是None

使用formatting_info=True打开工作簿。