Python win32com - 自动化Word - 如何替换文本框中的文本?
我正在尝试用Python自动化处理Word文档中的文本替换。(我使用的是Word 2003,Python 2.4,如果这有影响的话)
我下面的替换方法的第一部分可以正常工作,但就是无法处理文本框中的文本。文本框里的内容就是选不上。我注意到,当我手动进入Word并按下ctrl+A时,所有文本都会被选中,但文本框里的内容却不包括在内。
这是我目前的代码:
class Word:
def __init__(self,visible=0,screenupdating=0):
pythoncom.CoInitialize()
self.app=gencache.EnsureDispatch(WORD)
self.app.Visible = visible
self.app.DisplayAlerts = 0
self.app.ScreenUpdating = screenupdating
print 'Starting word'
def open(self,doc):
self.opendoc=os.path.basename(doc)
self.app.Documents.Open(FileName=doc)
def replace(self,source,target):
if target=='':target=' '
alltext=self.app.Documents(self.opendoc).Range(Start=0,End=self.app.Documents(self.opendoc).Characters.Count) #select all
alltext.Find.Text = source
alltext.Find.Replacement.Text = target
alltext.Find.Execute(Replace=1,Forward=True)
#Special handling to do replace in text boxes
#http://word.tips.net/Pages/T003879_Updating_a_Field_in_a_Text_Box.html
for shp in self.app.Documents(self.opendoc).Shapes:
if shp.TextFrame.HasText:
shp.TextFrame.TextRange.Find.Text = source
shp.TextFrame.TextRange.Find.Replacement.Text = target
shp.TextFrame.TextRange.Find.Execute(Replace=1,Forward=True)
#My Usage
word=Word(visible=1,screenupdating=1)
word.open(r'C:\Invoice Automation\testTB.doc')
word.replace('[PGN]','1')
在代码中,for shp in self.app .. 这一部分是我尝试处理文本框的部分。它似乎能找到文本框,但就是不替换里面的内容。
1 个回答
5
当我在Word文档中添加文本框时,这些文本框会被放在一个绘图画布里。所以,最外层的形状就是这个画布,而文本框则是在这个画布里面。你应该使用CanvasItems
这个方法来访问画布里的对象,也就是文本框。
下面这个例子对我来说是有效的。我创建了一个只有一个文本框的Word文档。
import win32com.client
word = win32com.client.Dispatch("Word.Application")
canvas = word.ActiveDocument.Shapes[0]
for item in canvas.CanvasItems:
print item.TextFrame.TextRange.Text
更新:回应提问者的评论。
我觉得你代码的问题在于,每一行包含Find
的代码都会创建一个新的Find
对象。你需要先创建一个Find
对象并给它一个名字,然后修改它的属性并执行它。所以在你的代码中应该这样写:
find = shp.TextFrame.TextRange.Find
find.Text = source
find.Replacement.Text = target
find.Execute(Replace=1, Forward=True)
或者可以用一行代码:
shp.TextFrame.TextRange.Find.Execute(FindText=source, ReplaceWith=target, Replace=1, Forward=True)
这两种方法在我的测试代码中都能正常工作。