import os#导入系统库
from win32com import client#导入WIN32库中的客户
from win32com.client import constants#读入客户中的常量库,是用来设格式的
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")#用WORD句柄打开文件
paragraphs=doc.Paragraphs#读出每一个段落
range1=paragraphs(1).Range#读出段落的范围
range1.Style=constants.wdStyleHeading1#让这段落的格式是标题1
range1.Style.Font.Name="华文新魏"#然后在标题二的格式上修改字体为楷体
range1.Style.Font.Color=0xFF0000#再修改字体颜色是0XFF0000
range1.Style.Font.Bold=1#最后加修加粗参数为1表示要加粗
range2=paragraphs(2).Range#读出第二段内容
range2.Style=constants.wdStyleHeading2#第二段内容格式设置为标题2号
#range2.ParagraphFormat.Alignment=constants.wdAlignParagraphRight
#根本就没有ParagraphFormat这个子类
range3=paragraphs(3).Range#读出第三段
range3.Style.Font.Size="50"#这段字体改为50SIZE
#doc.Close()
#word.Quit()
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()
import os
from win32com import client
from win32com.client import constants
word=client.gencache.EnsureDispatch('Word.Application')
word.Visible=1
word.DisplayAlerts=0
cpath=os.path.dirname(__file__)
doc=word.Documents.Open(cpath+"\\clipgraph.docx")
paragraphs=doc.Paragraphs
range1=paragraphs(5).Range#获得第五行范围
range1.InlineShapes.AddPicture(cpath+"\\cell.jpg",False,True)#插图
'''
AddPicture(_FileName_,_LinkToFile_,_SaveWithDocument_ ,_Range_ )
名称 必需/可选 数据类型 说明
FileName 必需 String 图片的路径和文件名。
LinkToFile 可选 Variant 真,要将图片链接到创建它的文件;假,使图片文件的独立副本[默认]
SaveWithDocument可选 Variant True则将链接的图片与文档一起保存。默认值为 False
Range 可选 Variant 图片置于文本中的位置。如该区域未折叠,图片将覆盖区域,否则插入图片[默认自动放置]
'''
#doc.Close()
#word.Quit()
#随机生成菜单
def getrandom2(n1,n2):
while True:
r1=random.randint(n1,n2)
r2=random.randint(n1,n2)
if r1!=r2:
break
return r1,r2
import os,random
from win32com import client
from win32com.client import constants
word=client.gencache.EnsureDispatch('Word.Application')
word.Visible=1
word.DisplayAlerts=0
cpath=os.path.dirname(__file__)
doc=word.Documents.Open(cpath+"\\food.docx")
range1=doc.Range(0,0)
range1.Style.Font.Size="16"
title="明星小学营养午餐菜单"
year1="2017年8月"
week=["一","二","三","四","五"]
teacher=['欧阳怡','翟定国','陈碧山','陈丽娟','郑怡衡','林邓超','朱健政','刘伟明','刘维基','梁维基','梁银燕']
rice=['糙米饭','白米饭','拌面']
vegetable=[" 毛豆白菜 " ," 豆芽菜 " ," 蛋香時瓜 " ," 高丽菜 " ," 佛手瓜 " ," 酸菜豆包 " ," 冬瓜 " ," 萝卜海带结 " ," 茄汁洋芋 " ," 家常豆腐 " ," 鲜菇花椰 " ," 豆皮三丝 " ," 伍彩雪莲 " ," 干香根丝 " ," 茄汁豆腐 " ," 香炒花椰 " ," 芹香粉丝 " ," 红萝卜 " ," 洋葱 " ," 青椒 " ]
meat=[" 糖醋排骨 " ," 美味大鸡腿 " ," 椒盐鱼条 " ," 香菇肉燥 " ," 宫保鸡丁 " ," 香卤腿排 " ," 梅干绞肉 " ," 香酥鱼丁 " ," 条瓜烧鸡 " ," 时瓜肉丝 " ," 海结卤肉 " ," 葱烧鸡 " ," 柳叶鱼 " ," 咖哩鸡肉 " ," 笋香鸡 " ," 沙茶猪柳 " ," 五香棒腿 " ," 三杯鸡丁 " ," 海结猪柳 " ," 茄汁鸡汁 " ]
soup=[" 蛋香木须汤 " ," 味噌海芽汤 " ," 绿豆汤 " ," 榨菜肉丝汤 " ," 姜丝海芽汤 " ," 枸杞爱玉汤 " ," 冬菜蛋花汤 " ," 冬瓜西米露 " ," 紫菜蛋花汤 " ," 蛋香木须汤 " ]
date1=1
weekday=2
while weekday<6 and date1 <31:
range1.InsertAfter(title+"\n")
range1.InsertAfter("日期:{}{}日(星期{})\n".format(year1,date1,week[weekday-1]))#此处必须用{}写法,也不知为何用课本的就报错
range1.InsertAfter("maker:"+teacher[random.randint(0,9)]+"teacher\n")
range1.InsertAfter("今日菜单:\n")
range1.InsertAfter("一、"+rice[random.randint(0,2)]+"\n")
rand1,rand2=getrandom2(0,19)
range1.InsertAfter("二、"+vegetable[rand1]+"\n")
range1.InsertAfter("三、"+vegetable[rand2]+"\n")
rand1,rand2==getrandom2(0,19)
range1.InsertAfter("四、"+meat[rand1]+"\n")
range1.InsertAfter("五、"+meat[rand2]+"\n")
range1.InsertAfter("六、"+soup[random.randint(0,9)]+"\n")
#range1.Collapse(constants.wdSectionBreakNextPage)#换页句不可用
weekday+=1
date1+=1
if weekday==6:
weekday=1
date1+=2
doc.SaveAs(cpath+"\\food.docx")
doc.Close()
word.Quit()