Openpyxl 不向文件添加新条目
Openpxl does not add new entries to File
我试图将新项目添加到现有的电子表格中,但它似乎不起作用。但是,该文件的 属性 与我在 python 中最近一次调用的时间戳相匹配。因为这是一个相当大的项目,所以可能需要一些背景信息来解决问题。
- 我正在使用 Openpyxl 2.5.3
- 至于现在,两个文件(*.py 和 *.xlsx)都在同一目录中
- 此外,它们目前存储在一个自动同步的 OneDrive 文件夹中
当尝试 运行
def add_value(self, to_position: Coordinate, new_value: str or int) -> bool:
try:
string = get_column_letter(to_position[0]) if isinstance(to_position[0], int) else to_position[0]
self.Sheet[f"{string}{to_position[1]}"].value = str(new_value)
workbook.save(self.Fullfilename)
return True
except (FileNotFoundError, IOError) as e:
print(f"{e}\n{filename} couldn't be found in current working directory\n[{os.listdir()}].")
return False
该函数的计算结果为 True
,但未对文件进行任何更改。
好吧,我真的很蠢。我的 __init__
构造函数带有一个名为 self.Workbook
的属性。但我拼错了 self.Worbook
,这又影响了 self.Sheet = self.WorBook.active
。请注意,我写了 workbook.save(self.Fullfilename)
,但我必须将更改保存到 self.Workbook.save(Fullfilename)
。最后一点,文件属性中 *.xlsx 的时间戳更新显示在更新日期(修改日期)旁边,这最初让我感到困惑,我的脚本是否真正访问或编辑了电子表格。
我试图将新项目添加到现有的电子表格中,但它似乎不起作用。但是,该文件的 属性 与我在 python 中最近一次调用的时间戳相匹配。因为这是一个相当大的项目,所以可能需要一些背景信息来解决问题。
- 我正在使用 Openpyxl 2.5.3
- 至于现在,两个文件(*.py 和 *.xlsx)都在同一目录中
- 此外,它们目前存储在一个自动同步的 OneDrive 文件夹中
当尝试 运行
def add_value(self, to_position: Coordinate, new_value: str or int) -> bool:
try:
string = get_column_letter(to_position[0]) if isinstance(to_position[0], int) else to_position[0]
self.Sheet[f"{string}{to_position[1]}"].value = str(new_value)
workbook.save(self.Fullfilename)
return True
except (FileNotFoundError, IOError) as e:
print(f"{e}\n{filename} couldn't be found in current working directory\n[{os.listdir()}].")
return False
该函数的计算结果为 True
,但未对文件进行任何更改。
好吧,我真的很蠢。我的 __init__
构造函数带有一个名为 self.Workbook
的属性。但我拼错了 self.Worbook
,这又影响了 self.Sheet = self.WorBook.active
。请注意,我写了 workbook.save(self.Fullfilename)
,但我必须将更改保存到 self.Workbook.save(Fullfilename)
。最后一点,文件属性中 *.xlsx 的时间戳更新显示在更新日期(修改日期)旁边,这最初让我感到困惑,我的脚本是否真正访问或编辑了电子表格。