Question

I wanna get a xlsx file's border use xlrd, for example:

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()

the information in console:

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

if I open a xlsx file like these codes:

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()

the information in console like these:

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

I don't know how to fix the bug. I need help.

Was it helpful?

Solution

Since you are passing formatting_info=False, xf_index for all cells would be None.

Open the workbook with formatting_info=True.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top