python读取word文件并替换部分文字,Python win32com-自动Word-如何替换文本框中的文本?...

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)

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

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.

解决方案

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

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

Update: answering OP's comment.

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)

Or a single line:

shp.TextFrame.TextRange.Find.Execute(FindText=source, ReplaceWith=target, Replace=1, Forward=True)

Both of these methods work in my test code.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值