#coding:utf-8
import win32com.client
import os
w = win32com.client.Dispatch('Word.Application')
w.Visible = 0
w.DisplayAlerts = 0
doc=w.Documents.Open(os.path.join(os.getcwd(),'cet.doc')) # 见注释一
#doc=w.Documents.Add()
#myRange = doc.Range(0,0)
#myRange.InsertBefore('Hello from Python!')
name = '××××××'.decode('utf-8')
year = '2010'
month = '12'
level = '六'.decode('utf-8')
id_no = '12347654436'
score = '428'
date = '二〇一〇年三月十六日'.decode('utf-8')
w.Selection.Find.ClearFormatting()
w.Selection.Find.Replacement.ClearFormatting()
# 查找word里面的name,替换为真实名字
w.Selection.Find.Execute('name', False, False, False, False, False, True, 1, True, name, 2)
w.Selection.Find.Execute('year', False, False, False, False, False, True, 1, True, year, 2)
w.Selection.Find.Execute('month', False, False, False, False, False, True, 1, True, month, 2)
w.Selection.Find.Execute('level', False, False, False, False, False, True, 1, True, level, 2)
w.Selection.Find.Execute('id_no', False, False, False, False, False, True, 1, True, id_no, 2)
w.Selection.Find.Execute('score', False, False, False, False, False, True, 1, True, score, 2)
w.Selection.Find.Execute('date', False, False, False,
#doc.Save() doc.SaveAs(os.path.join(os.getcwd(),'temp.doc')) doc.Close() w.Quit()
|