最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Modify Defined Names openpyxl - Stack Overflow

programmeradmin7浏览0评论

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 ?

发布评论

评论列表(0)

  1. 暂无评论