简体   繁体   English

无法保存由Openpyxl python3修改的现有Excel文档

[英]Unable to save existing excel document modified by Openpyxl python3

I am trying to edit an already existing excel document using openpyxl. 我正在尝试使用openpyxl编辑一个已经存在的excel文档。 After editing when I try saving the document it throws an error saying "'NoneType' object has no attribute 'read'". 编辑后,当我尝试保存文档时,它将引发错误,提示“'NoneType'对象没有属性'read'”。 I am quite sure that it is occurring while saving the document. 我非常确定在保存文档时会发生这种情况。 Following is my code - 以下是我的代码-

import openpyxl
db_path = r"C:\\Users\\mahapatr\\Desktop\\Newfolder"
iterationSheetName = 'MyIterationSheet.xlsx'
IterationSheetWorkbook=openpyxl.load_workbook(db_path+r'\\'+iterationSheetName)
IterationSheetSheet = IterationSheetWorkbook.active
print(IterationSheetSheet['F38'].value)
edgeRowNumber = '38'
IterationData = [1,2,3,4,5,6]
for cols,values in zip(['G','H','I','J','K','L'],IterationData):
        #d = octaneIterationSheetSheet.cell(row=edgeOctaneRowNumber,column=cols,value=int(values))
        print("Old Value",IterationSheetSheet[cols+edgeRowNumber].value)
        IterationSheetSheet[cols+edgeRowNumber].value = int(values)
        print("New Value",IterationSheetSheet[cols+edgeRowNumber].value)
IterationSheetWorkbook.save(db_path+r'\\'+'sample.xlsx')
IterationSheetWorkbook.close()

If anyone can point me to why Am I getting this error it will be of great help and will be greatly appreciated. 如果有人能指出我为什么会收到此错误,将对您有很大的帮助,我们将不胜感激。

I was using openpyxl version 2.6.0. 我正在使用openpyxl版本2.6.0。 Degrading it to 2.5.10 helped me. 将其降级为2.5.10可以帮助我。 There was nothing wrong in the code. 代码没有错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM