1.讀取Words檔案,顯示內容並利用Words內建的統計功能來計算字數、字元數、頁數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from win32com.client import Dispatch
import os
app = Dispatch("Word.Application")
app.Visible = 1
app.DisplayAlerts = 0
docx = app.Documents.Open(os.getcwd()+"\\test.docx")
print('段落數: ', docx.Paragraphs.count)
print('文章內容: ')
for i in range(len(docx.Paragraphs)):
para = docx.Paragraphs[i]
print(para.Range.text)
words = app.ActiveDocument.ComputeStatistics(0)
chars = app.ActiveDocument.ComputeStatistics(3)
pages = app.ActiveDocument.ComputeStatistics(2)
print('字數統計:', words)
print('字元統計:', chars)
print('頁數統計:', pages)
docx.Close()
app.Quit()
|
段落數: 3
文章內容:
國立虎尾科技大學 (英文:National Formosa University),簡稱虎科、虎科大、虎尾科大、NFU,別稱國立福爾摩沙大學,位於雲林縣虎尾鎮的國立科技大學。前身為雲林工專,為昔日三大工專之一。
目前設有目前有工程、管理、電機資訊、文理四個學院。為雲林國立大學聯盟以及臺灣國立大學系統成員。
(以上資料摘自維基百科)
字數統計: 137
字元統計: 161
頁數統計: 1
2. 把九九乘法表存到Excel檔案中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from win32com.client import Dispatch
import os
app = Dispatch("Excel.Application")
app.Visible = 1
app.DisplayAlerts = 0
xlsx = app.Workbooks.Add()
sheet = xlsx.Worksheets(1)
for i in range(1,10):
for j in range(1,10):
sheet.Cells(i, j).Value = i*j
xlsx.SaveAs(os.getcwd()+"\\99.xlsx")
xlsx.Close(False)
app.Quit()
|
執行結果:
3.讀取99.xls檔案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import win32com from win32com.client import Dispatch import os app = win32com.client.Dispatch("Excel.Application") app.Visible = 1 app.DisplayAlerts = 0 xlsx = app.Workbooks.Open(os.getcwd()+"\\99.xlsx") sheet = xlsx.Worksheets(1) row = sheet.UsedRange.Rows.Count col = sheet.UsedRange.Columns.Count for i in range(row): for j in range(col): if int(sheet.Cells(i+1, j+1).Value)<10: print(' ', end='') print(int(sheet.Cells(i+1, j+1).Value), end=' ') print() xlsx.Close(False) app.Quit() |
執行結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from win32com.client import Dispatch import time, os app = Dispatch("PowerPoint.Application") app.Visible = 1 app.DisplayAlerts = 0 pptx = app.Presentations.Open(os.getcwd()+"\\test.pptx") pptx.SlideShowSettings.Run() for i in range(len(pptx.slides)): time.sleep(1) pptx.SlideShowWindow.View.Next() pptx.SlideShowWindow.View.Exit() os.system('taskkill /F /IM POWERPNT.EXE') |


沒有留言:
張貼留言