使用pywin32从word文档中选择两个单词之间的文本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用pywin32从word文档中选择两个单词之间的文本相关的知识,希望对你有一定的参考价值。

而不是在这些关键字下找到关键字并插入文本(从列表中)。问题是关键字出现在文档中的多个位置,我想在特定位置的关键字下插入文本。

我使用pywin32从word文档中的表复制文本,而不是将其粘贴到同一文档上的特定关键字下。我想在两个关键字之间的特定范围/选择中搜索该关键字,其中一个关键字始终位于分节符之下。

代码:

def getRange(d_add):
    word.Documents.Open(d_add)
    doc = word.ActiveDocument

    rng1 = doc.Range
    print(rng1)
    if rng1.Find.Execute(FindText= "Introduction") == True:
        rng2 = doc.Range(rng1.Start, doc.Range.Start)
        if rng2.Find.Execute(FindText = "Conclusion") == True:
            myRange = rng1(rng1.End, rng2.Start).Text
    print(myRange)

产生以下错误:

if rng1.Find.Execute(FindText= "Introduction") == True:
AttributeError: 'function' object has no attribute 'Find'

但它存在于VBA(Find)中为什么不起作用?我需要导入win32com以外的其他模块吗?

答案

假设word定义如下:

from win32com.client import Dispatch
word = Dispatch("Word.Application")

或类似的东西,关于错误的问题的答案是你在Range这个词之后缺少括号。如果你用rng1 = doc.Range替换rng1 = doc.Range(),你就不会再出现这个错误了。

其余的代码也有一些错误。如果我理解你的代码的目的,那么这段代码应该工作:

def getRange(d_add):
    from win32com.client import Dispatch # not the only possibility
    word = Dispatch("Word.Application")
    word.Documents.Open(d_add)
    doc = word.ActiveDocument

    rng1 = doc.Range() # Select all your doc
    print(rng1)
    if rng1.Find.Execute(FindText= "Introduction") == True:
        # Now rng1 == "Introduction" and you want to search in your original doc 
        # for the word "Conclusion" after the word "Introduction"
        # so you need to specified the start point of your selection for rng2
        # being after the end of rng1
        rng2 = doc.Range(Start = rng1.End) # no End as you look until the end of the doc
        if rng2.Find.Execute(FindText = "Conclusion") == True:
            # Now rng1 == "Introduction" and rng2 == "Conclusion"
            # To select the text between these two words in your original text,
            # you to specified the start and end position in your range
            # being respectively after the end of rng1 and before the start of rng2
            # Note: the +1 helps to not take into account the first element after "Introduction"
            # probably being a space or a new line in the doc
            myRange = doc.Range(Start = rng1.End + 1, End = rng2.Start)
            print(myRange) # if not indented as the line myRange, an error occurred  
                           # if one of the two words looked for are not found,
                           # myRange would not be defined then

希望它能帮到你

以上是关于使用pywin32从word文档中选择两个单词之间的文本的主要内容,如果未能解决你的问题,请参考以下文章

如何使用SAS计算Word Mover的距离

Mac系统下Python自动化批量word转换pdf

用于在 = 和 ; 之间捕获单词的正则表达式

python操作word,关于win32com

pywin32安装步骤

Python:读取 .doc.docx 两种 Word 文件简述及“Word 未能引发事件”错误