保存工作簿时出现 OpenPyXL 错误:已存在具有相同名称和范围的 DefinedName
OpenPyXL error by saving of workbook: DefinedName with the same name and scope already exists
我想在 Python 中使用 OpenPyXL 编辑 Excel-XLSX 文件中的一个单元格,然后将此更改后的文件保存到具有其他名称的 xlsx 中。
但我收到以下错误:
回溯(最近调用最后):
File "E:\ExcelPythonTest\workspace\ExcelTest\ExcelTest.py", line 14, in <module>
book.save(path2)
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\workbook\workbook.py", line 294, in save
save_workbook(self, filename)
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\writer\excel.py", line 270, in save_workbook
writer.save(filename)
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\writer\excel.py", line 251, in save
self.write_data()
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\writer\excel.py", line 94, in write_data
archive.writestr(ARC_WORKBOOK, write_workbook(self.workbook))
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\writer\workbook.py", line 121, in write_workbook
defined_names.append(name)
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\workbook\defined_name.py", line 191, in append
raise ValueError("""DefinedName with the same name and scope already exists""")
ValueError: DefinedName with the same name and scope already exists
我的代码:
import os
import openpyxl
# Change this to suit
path = r'E:\ExcelPythonTest\example.xlsx'
path2 = r'E:\ExcelPythonTest\examle_UHAHAHAHAHHA.xlsx'
book = openpyxl.load_workbook( path )
sheet = book.get_sheet_by_name('Dictionary')
print sheet['G8'].value
sheet['G8'] = 3.18
print sheet['G8'].value
book.save(path2)
谁能帮帮我?
P.S:我试图用 C 和 c-library libxl 中的代码编辑相同的 .xlsx。所以我认为 xlsx 文件没有问题。
我在没有对 xlsx 进行任何编辑的情况下尝试了以下代码:
import os
import openpyxl
# Change this to suit
path = r'E:\ExcelPythonTest\Dt_RECORD_SHPS_RM_ROStruct.xlsx'
path2 = r'E:\ExcelPythonTest\Dt_RECORD_SHPS_RM_ROStruct_UHAHAHAHAHHA.xlsx'
book = openpyxl.load_workbook( path )
book.save(path2)
但是还是出现了同样的错误
我在工作中遇到了同样的问题,直到找到解决方案。您只需要在现有代码中添加一行。我不完全知道 definedName 对象有什么用,但生成的文件没有损坏(与没有该行不同)并且似乎没有因清除 definedNames 列表而导致的任何视觉差异。
import os
import openpyxl
# Change this to suit
path = r'E:\ExcelPythonTest\example.xlsx'
path2 = r'E:\ExcelPythonTest\examle_UHAHAHAHAHHA.xlsx'
book = openpyxl.load_workbook( path )
sheet = book.get_sheet_by_name('Dictionary')
print sheet['G8'].value
sheet['G8'] = 3.18
print sheet['G8'].value
# This fixes it
book.defined_names.definedName = []
book.save(path2)
我想在 Python 中使用 OpenPyXL 编辑 Excel-XLSX 文件中的一个单元格,然后将此更改后的文件保存到具有其他名称的 xlsx 中。 但我收到以下错误: 回溯(最近调用最后):
File "E:\ExcelPythonTest\workspace\ExcelTest\ExcelTest.py", line 14, in <module>
book.save(path2)
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\workbook\workbook.py", line 294, in save
save_workbook(self, filename)
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\writer\excel.py", line 270, in save_workbook
writer.save(filename)
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\writer\excel.py", line 251, in save
self.write_data()
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\writer\excel.py", line 94, in write_data
archive.writestr(ARC_WORKBOOK, write_workbook(self.workbook))
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\writer\workbook.py", line 121, in write_workbook
defined_names.append(name)
File "C:\Python27\lib\site-packages\openpyxl-2.4.0b1-py2.7.egg\openpyxl\workbook\defined_name.py", line 191, in append
raise ValueError("""DefinedName with the same name and scope already exists""")
ValueError: DefinedName with the same name and scope already exists
我的代码:
import os
import openpyxl
# Change this to suit
path = r'E:\ExcelPythonTest\example.xlsx'
path2 = r'E:\ExcelPythonTest\examle_UHAHAHAHAHHA.xlsx'
book = openpyxl.load_workbook( path )
sheet = book.get_sheet_by_name('Dictionary')
print sheet['G8'].value
sheet['G8'] = 3.18
print sheet['G8'].value
book.save(path2)
谁能帮帮我?
P.S:我试图用 C 和 c-library libxl 中的代码编辑相同的 .xlsx。所以我认为 xlsx 文件没有问题。
我在没有对 xlsx 进行任何编辑的情况下尝试了以下代码:
import os
import openpyxl
# Change this to suit
path = r'E:\ExcelPythonTest\Dt_RECORD_SHPS_RM_ROStruct.xlsx'
path2 = r'E:\ExcelPythonTest\Dt_RECORD_SHPS_RM_ROStruct_UHAHAHAHAHHA.xlsx'
book = openpyxl.load_workbook( path )
book.save(path2)
但是还是出现了同样的错误
我在工作中遇到了同样的问题,直到找到解决方案。您只需要在现有代码中添加一行。我不完全知道 definedName 对象有什么用,但生成的文件没有损坏(与没有该行不同)并且似乎没有因清除 definedNames 列表而导致的任何视觉差异。
import os
import openpyxl
# Change this to suit
path = r'E:\ExcelPythonTest\example.xlsx'
path2 = r'E:\ExcelPythonTest\examle_UHAHAHAHAHHA.xlsx'
book = openpyxl.load_workbook( path )
sheet = book.get_sheet_by_name('Dictionary')
print sheet['G8'].value
sheet['G8'] = 3.18
print sheet['G8'].value
# This fixes it
book.defined_names.definedName = []
book.save(path2)