Python win32com-自动Word-如何替换文本框中的文本? [英] Python win32com - Automating Word - How to replace text in a text box?

查看:936
本文介绍了Python win32com-自动Word-如何替换文本框中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python自动执行单词替换Word文档中的文本. (如果这很重要,我将使用Word 2003和Python 2.4)

I'm trying to automate word to replace text in a word document using Python. (I'm on word 2003 if that matters and Python 2.4)

下面的replace方法的第一部分适用于除文本框中的文本以外的所有内容.文本只是没有被选中.我注意到当我手动进入Word并按ctrl-A时,除文本框外,所有其他文本均被选中.

The first part of my replace method below works on everything except text in text boxes. The text just doesn't get selected. I notice when I go into Word manually and hit ctrl-A all of the text gets selected except for the text box.

到目前为止,这是我的代码:

Here's my code so far:

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')

self.app ..部分中的for shp是我尝试打文本框的尝试.似乎找到了文本框,但它并没有替代任何内容.

The for shp in self.app .. section is my attempt to hit the text boxes. It seems to find the text box, but it doesn't replace anything.

推荐答案

当我将文本框添加到Word文档时,它们被添加到绘图画布中.因此,顶层形状是画布,并且文本框包含在画布中.您应该使用CanvasItems方法访问画布中的对象,即文本框

When I add text boxes to a word document, they are added inside a drawing canvas. Therefore the top level shape is the canvas, and the text boxes are contained within the canvas. You should use the CanvasItems method to access the objects in the canvas, ie the text boxes

以下示例对我有用.我用一个文本框创建了一个Word文档.

The following example works for me. I created a word document with a single text box.

import win32com.client

word = win32com.client.Dispatch("Word.Application")
canvas = word.ActiveDocument.Shapes[0]
for item in canvas.CanvasItems:
    print item.TextFrame.TextRange.Text


更新:回答OP的评论.

我认为您的代码的问题在于,带有Find的每一行代码都会创建一个新的Find对象.您必须创建Find对象并将其绑定到名称,然后修改其属性并执行它.因此,在您的代码中,您应该具有:

I think the problem with your code is that each line of code with Find creates a new Find object. You have to create and bind a Find object to a name, then modify its attributes and execute it. So in your code you should have:

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)

这两种方法都可以在我的测试代码中使用.

Both of these methods work in my test code.

这篇关于Python win32com-自动Word-如何替换文本框中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆