Pyexcel不操纵我告诉它的细胞

我正在使用pyexcel来自动打开一个excel表格,处理一些数据并再次保存。 然而,它只能操纵第一个命令,似乎忽略了其他命令。

我访问我的文件,

book = pyexcel.get_book(file_name=file_to_be_manipulated) 

而file_to_be_manipulated则保存到文件的链接

那么我有我的工作表像一个元组

 sheets = ('first_sheet', 'second_sheet', etc.) 

并通过它们访问它们

 sheet_name = book[sheets[sheet_index]] 

遍历我想要操作的单元格我访问单元格

这里一切正常,我只要遍历第二列,然后'删除'前两列中的所有内容。 这工作得很好。

  row = 5 column = 2 column_to_be_deleted = 0 second_column_to_be_deleted = 1 sheet_name = book[sheets[sheet_index]] while sheet_name[row,column] != None: row_to_be_deleted = row second_row_to_be_deleted = row sheet_name[row_to_be_deleted, column_to_be_deleted] = "" sheet_name[second_row_to_be_deleted, second_column_to_be_deleted] = "" row += 1 

但是,在这里奇怪,我只是想操纵列2和3从'空'到'默认'和'x',但这是行不通的。 第一列的“删除”工作正常,但其他两个操作将无法正常工作,我不明白为什么。

  row = 5 column = 1 column_to_be_deleted = 0 column_to_set_to_default = 2 column_to_set_to_something = 3 sheet_name = book[sheets[sheet_index]] while sheet_name[row,column] != None: row_to_be_deleted = row row_to_set_to_default = row row_to_set_to_something = row sheet_name[row_to_be_deleted, column_to_be_deleted] = "" sheet_name[row_to_set_to_default, column_to_set_to_default] = "Default" sheet_name[row_to_set_to_something, column_to_set_to_something] = "x" row += 1 

它只会工作,如果一些string已经是在列2和3,那么它工作正常。

但是,在这里我想改变列11行5的值为'1',只是像删除其他例子中删除第一列。 这里的删除工作正常,但第11行第5行中的“0”不会更改为“1”

  if sheet_index == 13: #ORGANISATIONS SHEET, L6 MUST BE SET TO 1 row = 5 column = 1 column_to_be_deleted = 0 column_to_set_to_one = 11 row_to_set_to_one = 5 sheet_name = book[sheets[sheet_index]] sheet_name[row_to_set_to_one, column_to_set_to_one] = "1" while sheet_name[row,column] != None: row_to_be_deleted = row sheet_name[row_to_be_deleted, column_to_be_deleted] = "" row += 1 

这是怎么回事,对我来说这似乎是随机的哪个命令执行,哪个不是。

问题似乎是pyexcel如何看待Excel表格。 Pyexcel首先查看表单的最后一个数据条目的位置。

然后它创build一个这么大的数组,如果你想操作这个数组之外的数据,它不会抛出一个错误,但根本不会做它被要求的。

所以,如果你想在一个没有填充数据的列中操作数据,你必须创build这个列(如何做到这一点,从pyexcel中看到readtheds),或者首先手动input一些数据。