使用pywin32从word文档中选择两个单词之间的文本 [英] Selecting text between two words from word document using pywin32
问题描述
而不是查找关键字并在这些关键字下插入文本(从列表中).问题是关键字出现在文档中的多个位置,我想在特定位置的关键字下插入文本.
than finding keywords and inserting text (from a list) under those keywords. the problem is that the keywords appear in more than one place in the document and I want to insert the text under keywords that are in specific places.
我正在使用 pywin32 从 Word 文档的表格中复制文本,而不是将其粘贴到同一文档的特定关键字下.我想在两个关键字之间的特定范围/选择中搜索该关键字,其中一个将始终位于分节符下.
I am using pywin32 to copy text from a table in a word document than paste it on under a specific keyword on the same document. I want to search for that keyword in a specific range/ selection that will be between two keywords, one of which will always be under a section break.
代码:
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 以外的其他模块吗?
But it exists in VBA (Find) why doesn't it work? do I need to import another module other than win32com ?
推荐答案
假设 word
的定义如:
from win32com.client import Dispatch
word = Dispatch("Word.Application")
或类似的东西,您对错误的问题的答案是您在单词 Range
之后缺少括号.如果您将 rng1 = doc.Range
替换为 rng1 = doc.Range()
,您将不会再收到此错误.
or something equivalent, the answer to your question on the error is you are missing parentheses after the word Range
. If you replace rng1 = doc.Range
by rng1 = doc.Range()
you won't get this error anymore.
其余的代码也有一些错误.如果我很好地理解您的代码的目的,那么此代码应该可以工作:
The rest of the code has some errors too. If I understand well the purpose of your code, then this code should work:
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文档中选择两个单词之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!