操作

import win32com.client as wc 
import os
#Word文件
app = wc.Dispatch('Word.Application') #綁定word文件
app.Visible = 1 #顯示Word軟體(0為不顯示)
app.DisplayAlerts=0 #不顯示警告
doc = app.Documents.Open('filepath') #打開Word文件
newDoc = app.Documents.Add() #開啟一個新的Word文件。
app.Quit() #關閉MS Word.
doc.PrintOut() #列印文件
#Excel文件
app = wc.Dispatch('Excel.Application') #綁定excel表格
xls = app.WorkBooks.Open('filepath') #打開Excel文件
#插入文字
range = doc.Range(0, 0) # 設定至文件的最開頭
range.InsertBefore('在range前面插入的文字')
range.InsertAfter('在range後面插入的文字')
#設定文字格式
range.Style.Font.Size = "16" # 設定字體尺寸=16
range.Style = constants.wdStyleHeading1 # 設定style為Heading 1
range.Style = constants.wdStyleHeading2 # 設定style為Heading 2
range.Style = constants.wdStyleHeading3 # 設定style為Heading 3
range.ParagraphFormat.Alignment = constants.wdAlignParagraphLeft
#設定段落為靠左
range.ParagraphFormat.Alignment = constants.wdAlignParagraphRight
#設定段落為靠右
range.ParagraphFormat.Alignment = constants.wdAlignParagraphCenter
#設定段落為置中
range.ParagraphFormat.Alignment = constants.wdAlignParagraphJustify #設定段落為左右對齊range.Style.Font.Name = "Arial" # 設定字型為Arial
range.Style.Font.Name = "Time New Roman" # 設定字型為Time New Roman
range.Style.Font.Name = "標楷體" # 設定字型為標楷體
range.Style.Font.Color = 0xFF0000 # 設定字型的顏色為Blue
range.Style.Font.Color = 0x00FF00 # 設定字型的顏色為Green
range.Style.Font.Color = 0x0000FF # 設定字型的顏色為Red
range.Style.Font.Bold = 1 # 設定字型為粗體字
range.Style.Font.Italic = 1 # 設定字型為斜體字
range.Style.Font.Underline = 1 # 為字型加底線
range.Style.Font.Shadow = 1 # 為字型加陰影
range.Style.Font.Outline = 1 # 為字型加外框
range1 = paragraphs(1).Range # 設定第一段落
range2 = paragraphs(2).Range # 設定第二段落
range3.Style.Font.Size = "10" # 設定第三段落的文字尺寸
#設定表格
table = doc.Tables.Add(range, 3, 4) # 新增一個3x4表格
table.Cell(1,1).Range.InsertAfter('Some text') # 新增文字到cell(1,1)
table.Cell(1,1).Range.Font.Name = "Arial" # 設定字型為Arial
table.Cell(1,1).Range.Font.Color = 0xFF0000 # 設定字型為blue
table.Rows.Add() # 新增一個Row
table.Columns.Add() # 新增一個Column
table.Rows(n).Delete() # 刪除第n個Row
table.Columns(n).Delete() # 刪除第n個Columns
#轉換為html
wc1 = wc.constants
app.ActiveDocument.WebOptions.RelyOnCSS = 1
app.ActiveDocument.WebOptions.OptimizeForBrowser = 1
app.ActiveDocument.WebOptions.BrowserLevel = 0
app.ActiveDocument.WebOptions.OrganizeInFolder = 0
app.ActiveDocument.WebOptions.UseLongFileNames = 1
app.ActiveDocument.WebOptions.RelyOnVML = 0
app.ActiveDocument.WebOptions.AllowPNG = 1
app.ActiveDocument.SaveAs( FileName = filenameout, FileFormat = wc1.wdFormatHTML )
#正文文字取代
app.Selection.Find.ClearFormatting()
app.Selection.Find.Replacement.ClearFormatting()
app.Selection.Find.Execute('要被取代的文字', False, False, False, False, False, True, 1, True, '換上的文字', 2)
#存檔並關閉文件
doc.SaveAs("E:\\text.doc")
doc.Close()

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store