I am coding a function which convert an xlsx file with a template into an other template. One brick of my tool is the duplication of the name manager by copying workbook.defined_names. Here is my function :
def copy_defined_names(source_file, target_file, output_file):
try:
wb_source = load_workbook(source_file, data_only=False,keep_vba=True,keep_links=True,rich_text=True)
wb_target = load_workbook(target_file, data_only=False,keep_vba=True,keep_links=True,rich_text=True)
# Copier les noms définis du classeur source vers le classeur cible
for key in wb_source.defined_names.keys():
defined_name_source = wb_source.defined_names[key]
defn=DefinedName(key,attr_text=defined_name_source.attr_text)
wb_target.defined_names[key]=defn
wb_target.save(output_file)
except Exception as e:
print(f"Error ": {e}")
But when I try to open the output_file I have an error message that tells me that it is impossible to open the file. Have you already encountered this problem when modifying the name manager of an xlsx file ?