1. 程式人生 > >吳裕雄 實戰PYTHON編程(7)

吳裕雄 實戰PYTHON編程(7)

不重復 display int tab img documents 作者 weekday visible

import os

from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 1
word.DisplayAlerts = 0
doc = word.Documents.Add()
range1 = doc.Range(0,0) #文件起始處
range1.InsertAfter("這是測試第一行\n這是測試第二行\n")
range1.InsertAfter("這是測試第三行\n這是測試第四行\n")
range1.InsertBefore("第一次插入到文件最前方\n")
range1.InsertBefore("再次插入到文件最前方\n")
# cpath = os.path.dirname(__file__)
doc.SaveAs("F:\\pythonBase\\pythonex\\test1.docx")
doc.Close()
word.Quit()

import os

from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 0
word.DisplayAlerts = 0
# cpath=os.path.dirname(__file__)
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\test1.docx")
print(doc.Content)
doc.Close()
word.Quit()

import os

from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 0
word.DisplayAlerts = 0
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\test1.docx")
paragraphs = doc.Paragraphs
for p in paragraphs:
text = p.Range.Text.strip()
print(text)
doc.Close()
word.Quit()

技術分享圖片

import os

from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 0
word.DisplayAlerts = 0
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\test1.docx")
paragraphs = doc.Paragraphs
print("第一段:" + paragraphs(1).Range.Text.strip())
print("第三段:" + paragraphs(3).Range.Text.strip())
doc.Close()
word.Quit()

技術分享圖片

import os
import time

from win32com import client
from win32com.client import constants

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 1
word.DisplayAlerts = 0
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\test1.docx")
paragraphs = doc.Paragraphs
range1 = paragraphs(1).Range #第1段
range1.Style = constants.wdStyleHeading1
range1.Style.Font.Name = "標楷體"
range1.Style.Font.Color = 0xFF0000 #藍色
range1.Style.Font.Bold = 1 #Italic, Underline, Shadow, Outline

range2 = paragraphs(2).Range #第2段
range2.Style = constants.wdStyleHeading3
range2.ParagraphFormat.Alignment = constants.wdAlignParagraphRight

range3 = paragraphs(3).Range #第3段
range3.Style.Font.Size = "10"
doc.Close()
word.Quit()
time.sleep(3)

import os
from win32com import client

word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 1
word.DisplayAlerts = 0
doc = word.Documents.Open("F:\\pythonBase\\pythonex\\08\\clipgraph.docx")
data = [ ["型號", "尺寸", "顏色", "價格"], ["A8", "5.0英寸", "白色", "8000"],["A10", "5.5 英寸", "金黃", "22000"] ]
paragraphs = doc.Paragraphs #讀取所有段落
range1 = paragraphs(4).Range #取第4段的起止範圍
table = doc.Tables.Add(range1, 3, 4) #在第4段之前插入一個3行4列表格
for i in range(1,table.Rows.Count+1): #i取值分別為1~3。切記rang()函數的特性
print(i)
# for j in range(1,table.Columns.Count+1): #j取值分別為1~4
# table.Cell(i,j).Range.Text = data[i-1][j-1] #第一次會把data[0][0],即"型號",插入Cell(1,1)
#table.Cell(2,3).Range.Font.Color = 0x0000FF #設置第2行第3列單元格的字體顏色
#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.Open("F:\\pythonBase\\pythonex\\clipgraph.docx")
paragraphs = doc.Paragraphs
range1 = paragraphs(4).Range
range1.InlineShapes.AddPicture(cpath + "F:\\pythonBase\\pythonex\\ch08\\media\\cell.jpg", False, True)
#doc.Close()
#word.Quit()

import os
from win32com import client as 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 + "\\media\\clipgraph.docx")
word.Selection.Find.ClearFormatting()
word.Selection.Find.Replacement.ClearFormatting()
word.Selection.Find.Execute("方法",False,False,False,False,False,True,constants.wdFindContinue,False,"method",constants.wdReplaceAll)
#doc.Close()
#word.Quit()

def getrandom2(n1, n2): #取得2個不重復的隨機值
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 #不顯示警告
doc = word.Documents.Add()
range1 = doc.Range(0,0) #文件開頭
range1.Style.Font.Size = "16" #字體大小
title = "明星小學營養午餐菜單"
year1 = "2017年8月"
week = ["一","二","三","四","五"]
teacher = ["歐陽怡","翟定國","陳碧山","陳麗娟","鄭怡衡","林鄧超","朱健政","劉偉明","劉維基","梁銀燕"]
rice = ["糙米飯","白米飯","拌面"]
vegetable = ["毛豆白菜","豆芽菜","蛋香時瓜","高麗菜","佛手瓜","酸菜豆包","冬瓜","蘿蔔海帶結","茄汁洋芋","家常豆腐","鮮菇花椰","豆皮三絲","伍彩雪蓮","幹香根絲","茄汁豆腐","香炒花椰","芹香粉絲","紅蘿蔔","洋蔥","青椒"]
meat = ["糖醋排骨","美味大雞腿","椒鹽魚條","香菇肉燥","宮保雞丁","香鹵腿排","梅幹絞肉","香酥魚丁","條瓜燒雞","時瓜肉絲","海結鹵肉","蔥燒雞","柳葉魚","咖哩絞肉","筍香雞","沙茶豬柳","五香棒腿","三杯雞丁","海結豬柳","茄汁雞丁"]
soup = ["蛋香木須湯","味噌海芽湯","綠豆湯","榨菜肉絲湯","姜絲海芽湯","枸杞愛玉湯","冬菜蛋花湯","冬瓜西米露","紫菜蛋花湯","蛋香木須湯"]
date1= 1 #開始日期為1日
weekday = 2 #開始日期為星期六

while weekday < 6 and date1 < 31: #周一到周五及30日前才制作菜單
range1.InsertAfter(title + "\n")
range1.InsertAfter("日期:" + year1 + str(date1) + "日 (星期" + week[weekday-1] + ")\n")
range1.InsertAfter("制作者:" + teacher[random.randint(0,9)] + "老師\n") #10位老師中隨機選一位
range1.InsertAfter("今日菜單:\n")
range1.InsertAfter("一、" + rice[random.randint(0,2)] + "\n") #取1個隨機數作為主食列表下標
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.wdCollapseEnd) #移到range尾
range1.InsertBreak(constants.wdSectionBreakNextPage) #換頁
weekday += 1 #星期加1
date1 += 1 #日期加1
if weekday == 6: #如果是星期六
weekday = 1 #設為星期一
date1 += 2 #日期加2(星期六及星期日)

cpath=os.path.dirname(__file__)
doc.SaveAs(cpath + "\\media\\food.docx") #獲取文件保存路徑並把文件保存為food.docx
#doc.Close()
#word.Quit()

import os
from win32com import client
from win32com.client import constants
word = client.gencache.EnsureDispatch(‘Word.Application‘)
word.Visible = 0
word.DisplayAlerts = 0
runpath = os.path.dirname(__file__) + "\\replace" #獲取replace文件夾的路徑
tree = os.walk(runpath) #取得目錄樹
print("所有 Word 文件:")
for dirname, subdir, files in tree:
allfiles = []
for file in files: # 取得所有.docx .doc文件,存入allfiles列表中
ext = file.split(".")[-1] #取得文件名後綴
if ext=="docx" or ext=="doc": #取得所有.docx .doc文件
allfiles.append(dirname + ‘\\‘ + file) #加入allfiles列表

if len(allfiles) > 0: #如果有符合條件的文件
for dfile in allfiles:
print(dfile)
doc = word.Documents.Open(dfile) #打開文件
word.Selection.Find.ClearFormatting()
word.Selection.Find.Replacement.ClearFormatting()
word.Selection.Find.Execute("方法",False,False,False,False,False,True,constants.wdFindContinue,False,"method",constants.wdReplaceAll)
doc.Close()
word.Quit()

吳裕雄 實戰PYTHON編程(7)