而不是在这些关键字下查找关键字并插入文本(从列表中)。 问题是关键字出现在文档中的多个位置,我想在特定位置的关键字下插入文本。
我使用pywin32从word文档中的表复制文本,而不是将其粘贴到同一文档中的特定关键字下。我想在两个关键字之间的特定范围/选择中搜索该关键字,其中一个关键字总是在分节符之下。
代码:
import threading
import time
import subprocess
def thread_do_downloads(stop_activated):
p = subprocess.Popen('./script.py', stdout=subprocess.PIPE)
while p.poll() is None:
time.sleep(0.5)
print("Subprocess still running... Slepping a bit... ZzzzzzZZZ")
if stop_activated.is_set():
print("Forcing output requested!!!")
print("Trying to terminate the process nicely, which a SIGTERM:")
p.terminate()
time.sleep(0.5)
if p.poll() is None:
print("Not being nice anymore... Die, die die!!")
p.kill()
print("This is what the subprocess 'said':\n%s" % p.stdout.read())
return
print("stopping normally")
def do_stuff_with_downloaded_data():
print("doing stuff with downloaded data")
def listen_for_keypress(stop_activated):
input("Press ENTER to stop downloading.")
print("keypress intercepted")
stop_activated.set()
def main():
stop_activated = threading.Event()
dl = threading.Thread(target=thread_do_downloads, args=(stop_activated,))
dl.start()
kill_listener = threading.Thread(target=listen_for_keypress, args=(stop_activated,), daemon=True)
kill_listener.start()
dl.join()
print("Finished downloading data")
# here are some lines to make sure the download thread above completes gracefully
do_stuff_with_downloaded_data()
print("All done")
if __name__ == '__main__':
main()
产生以下错误:
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)
但它存在于VBA(Find)中为什么不起作用?我是否需要导入win32com以外的其他模块?
答案 0 :(得分:0)
假设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
希望它可以帮到你