如是326 2024-05-03 21:45 采纳率: 0%
浏览 31

pywin32文件自动化报错'集合所要求的成员不存在。'

我最近在学习pywin32文件自动化的相关知识,我想通过word“引用”板块中的“目录”板块中的“自动目录”来为我自己的文档生成目录,在使用了宏录制之后得到了以下VB代码:


Application.Templates( _
"C:\Users\北嘉图\AppData\Roaming\Microsoft\Document Building Blocks\2052\16\Built-In Building Blocks.dotx" _
).BuildingBlockEntries("自动目录 1").Insert Where:=Selection.Range, RichText _
:=True

我将其改写为Python代码,全部代码如下:


from win32com import client

docFilePath = r"C:\Users\北嘉图\Desktop\社会科学概论.docx"
wordExe = client.Dispatch("Word.Application")
wordExe.Visible = 1
wordExe.DisplayAlerts = 0

docFile = wordExe.Documents.Open(docFilePath)

# 添加目录
wordExe.Selection.GoTo(0, 0, 0, 0)  # 将光标移动到文档开头
tocTemplate = r"C:\Users\北嘉图\AppData\Roaming\Microsoft\Document Building Blocks\2052\16\Built-In Building Blocks.dotx"
tocName = "自动目录 1"
wordExe.Application.Templates(tocTemplate).BuildingBlockEntries(tocName).Insert(Where=wordExe.Selection.Range, RichText=True)

但是获得报错:


Templates.py_, line 54, in __call__    ret = self._oleobj_.InvokeTypes(0, LCID, 1, (9, 0), ((16396, 1),),Indexpywintypes.com_error_ (-2147352567, '发生意外。', (0, 'Microsoft Word', '集合所要求的成员不存在。', 'wdmain11.chm', 25421, -2146822347), None)

进程已结束,退出代码为 1
我猜想是由于参数没有写好导致的,请问该问题如何解决?

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-05-03 21:45
    关注

    引自免费微信小程序:皆我百晓生

    根据错误信息,“集合所要求的成员不存在”通常意味着你在尝试访问的对象或者属性不存在。在这种情况下,问题可能出在wordExe.Application.Templates(tocTemplate)这一行,即模板对象可能未正确找到或加载。

    为了调试这个问题,我们可以分步检查:

    1. 确保模板文件路径正确无误。
    2. 使用Exists方法检查模板是否存在。
    3. 如果模板存在,尝试获取并操作模板对象。

    以下是修改后的代码,添加了额外的检查步骤:

    import os
    
    from win32com.client import constants as w Constants
    
    def check_template_exists(template_path):
        return os.path.exists(template_path)
    
    def get_or_create_toc(word_app, template_path, toc_name):
        templates = word_app.Templates()
        
        if not check_template_exists(template_path):
            print(f"模板文件 {template_path} 不存在.")
            return None
        
        for template in templates:
            if template.Name == os.path.basename(template_path):
                return template.BuildingBlockEntries(toc_name)
        
        print(f"模板中找不到名为 '{toc_name}' 的目录条目.")
        return None
    
    docFilePath = r"C:\Users\北嘉图\Desktop\社会科学概论.docx"
    wordApp = client.Dispatch("Word.Application")
    wordApp.Visible = 1
    wordApp.DisplayAlerts = 0
    
    docFile = wordApp.Documents.Open(docFilePath)
    
    # 添加目录
    wordApp.Selection.GoTo(0, 0, 0, 0)  # 将光标移动到文档开头
    tocTemplate = r"C:\Users\北嘉图\AppData\Roaming\Microsoft\Document Building Blocks\2052\16\Built-In Building Blocks.dotx"
    tocName = "自动目录 1"
    
    # 检查模板和条目
    toc_entry = get_or_create_toc(wordApp, tocTemplate, tocName)
    if toc_entry is not None:
        toc_entry.Insert(Where=wordApp.Selection.Range, RichText=True)
    else:
        print("插入目录失败.")
    

    这段代码首先定义了一个辅助函数check_template_exists用于检查模板文件是否存在,然后定义了一个函数get_or_create_toc来获取模板及其对应的目录条目。这样可以确保在插入目录之前,我们已经正确地找到了模板和条目。

    运行这段代码,如果还有问题,它将输出更详细的错误消息。如果一切顺利,你应该能看到目录被成功插入到文档中。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月3日