import os
from win32com import client as client
from win32com.client import constants
word=client.gencache.EnsureDispatch('Word.Application')#获WORD句柄
word.Visible=1#可见
word.DisplayAlerts=0#关警告(有修改的话关闭时会提示,置0默认确定)
cpath=os.path.dirname(__file__)#获路径
doc=word.Documents.Open(cpath+"\\clipgraph.docx")#开文件
word.Selection.Find.Execute("我是",False,False,False,False,False,True,
constants.wdFindContinue,False,"XXX",2)#把"我是"改为"method"
#枚举常量不可用!要直接写数字:0不替换!1换一次!2无限换
#doc.Close()
#word.Quit()
import os
from win32com import client
word=client.gencache.EnsureDispatch('Word.Application')#开WORD句柄
word.Visible=1#可见
word.DisplayAlerts=0#无警告
cpath=os.path.dirname(__file__)#获路径
doc=word.Documents.Open(cpath+"\\test1.docx")#开文件
print(doc.Content)#事实证明直接读会有错误!输出:这是测试第四行件最前方,不知是什么
paragraphs=doc.Paragraphs#读出每一行
for p in paragraphs:#遍历
text=p.Range.Text.strip()#获得此行文本
print(text)#输出
print("first: "+paragraphs(1).Range.Text.strip())#读出第一行
print("third: "+paragraphs(3).Range.Text.strip())#读出第三行
import os
from win32com import client
word=client.gencache.EnsureDispatch('Word.Application')#WORD句柄
word.Visible=0#不可见
word.DisplayAlerts=0#不警告
runpath=os.path.dirname(__file__)+"\\replace"#获得路径,尾后加上\\replace
tree=os.walk(runpath)#通过在目录树中游走输出在目录中的文件名,向上或者向下
print("all the word files:")#提示语
for dirname,subdir,files in tree:#读出树下的三个元素值
allfiles=[]#开列表
for file in files:#遍历
ext=file.split(".")[-1]#获得以.分间的最后一个串,即后缀名
if ext=="docx" or ext=="doc":#有两种后缀
allfiles.append(dirname+'\\'+file)#如果有,就把它加进去
if len(allfiles)>0:
for dfile in allfiles:#遍历每个文件
print(dfile)#打印文件内容
doc=word.Cocuments.Open(dfile)#然后打开
word.Selection.Find.ClearFormatting()#清除活动文档中的所有文本格式和段落格式
word.Selection.Find.Replacement.ClearFormatting()#清除剪贴版中文本内容格式
word.Selection.Find.EXECUTE("XXYYXX",False,False,
False,False,False,True,1,False,"yyxxyy",2)#然后把里面的XXYYXX改为2
doc.Close()
word.Quit()
import os#系统库用于读路径
from win32com import client#client用于开WORD句柄
word=client.gencache.EnsureDispatch('Word.Application')#获WORD句柄
word.Visible=1#可见
word.DisplayAlerts=0#不警告
cpath=os.path.dirname(__file__)#获路径
doc=word.Documents.Open(cpath+"\\clipgraph.docx")#打开文件
data=[ ["型号","大小","颜色","价格"],
["A8","5.0英寸","白色","8000"],
["A10","5.5英寸","金黄","22000"]]#开列表
paragraphs=doc.Paragraphs#遍历
range1=paragraphs(4).Range#获第四行内容
table=doc.Tables.Add(range1,3,4)#然后把range1的内容改为一个3*4的表格即table
for i in range(1,table.Rows.Count+1):#逐行遍历,注意结束下标要+1
for j in range(1,table.Columns.Count+1):#逐列遍历,注意结束下标要+1
table.Cell(i,j).Range.Text=data[i-1][j-1]#然后赋值,cell是细胞
table.Cell(2.3).Range.Font.Color=0xFF00FF#然后还可以给字赋色
#doc.Close()
#word.Quit()
import os
from win32com import client
word=client.gencache.EnsureDispatch('Word.Application')
word.Visible=1
word.DisplayAlerts=0
doc=word.Documents.Add()#创新一个WORD文档句柄
range1=doc.Range(0,0)#初始化一个范围是开头(第0行第0个字符)
range1.InsertAfter("这是测试第一行\n这是测试第二行\n")#在文末插入
range1.InsertAfter("这是测试第三行\n这是测试第四行\n")#在文末插入
range1.InsertBefore("第一次插入到文件最前方\n")#在文首插入
range1.InsertBefore("第二次插入到文件最前方\n")#在文首插入
cpath=os.path.dirname(__file__)#获路径
doc.SaveAs(cpath+"\\test1.docx")#设置文件夹保存
#doc.Close()
#word.Quit()