我想使用 xlrd 获取 xlsx 文件的边框,例如:
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 (most recent call last): File "judge_merged.py", line 68, in fmt = book.xf_list[sheet.cell(row, col).xf_index] TypeError: list indices must be integers, not NoneType
我不知道如何修复这个错误。我需要帮助。
请您参考如下方法:
由于您要传递 formatting_info=False,因此所有单元格的 xf_index 都将为 None。
使用 formatting_info=True 打开工作簿。
