python實(shí)現(xiàn)在windows下操作word的方法
更新時(shí)間:2015年04月28日 09:48:39 作者:重負(fù)在身
這篇文章主要介紹了python實(shí)現(xiàn)在windows下操作word的方法,涉及Python操作word實(shí)現(xiàn)打開(kāi)、插入、轉(zhuǎn)換、打印等操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了python實(shí)現(xiàn)在windows下操作word的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
import win32com from win32com.client import Dispatch, constants w = win32com.client.Dispatch('Word.Application') # 或者使用下面的方法,使用啟動(dòng)獨(dú)立的進(jìn)程: # w = win32com.client.DispatchEx('Word.Application') # 后臺(tái)運(yùn)行,不顯示,不警告 w.Visible = 0 w.DisplayAlerts = 0 # 打開(kāi)新的文件 doc = w.Documents.Open( FileName = filenamein ) # worddoc = w.Documents.Add() # 創(chuàng)建新的文檔 # 插入文字 myRange = doc.Range(0,0) myRange.InsertBefore('Hello from Python!') # 使用樣式 wordSel = myRange.Select() wordSel.Style = constants.wdStyleHeading1 # 正文文字替換 w.Selection.Find.ClearFormatting() w.Selection.Find.Replacement.ClearFormatting() w.Selection.Find.Execute(OldStr,False,False,False,False,False,True,1,True,NewStr,2) # 頁(yè)眉文字替換 w.ActiveDocument.Sections[0].Headers[0].Range.Find.ClearFormatting() w.ActiveDocument.Sections[0].Headers[0].Range.Find.Replacement.ClearFormatting() w.ActiveDocument.Sections[0].Headers[0].Range.Find.Execute(OldStr,False,False,False,False,False,True,1,False,NewStr,2) # 表格操作 doc.Tables[0].Rows[0].Cells[0].Range.Text ='123123' worddoc.Tables[0].Rows.Add() # 增加一行 # 轉(zhuǎn)換為html wc = win32com.client.constants w.ActiveDocument.WebOptions.RelyOnCSS = 1 w.ActiveDocument.WebOptions.OptimizeForBrowser = 1 w.ActiveDocument.WebOptions.BrowserLevel = 0 # constants.wdBrowserLevelV4 w.ActiveDocument.WebOptions.OrganizeInFolder = 0 w.ActiveDocument.WebOptions.UseLongFileNames = 1 w.ActiveDocument.WebOptions.RelyOnVML = 0 w.ActiveDocument.WebOptions.AllowPNG = 1 w.ActiveDocument.SaveAs( FileName = filenameout, FileFormat = wc.wdFormatHTML ) # 打印 doc.PrintOut() # 關(guān)閉 # doc.Close() w.Documents.Close(wc.wdDoNotSaveChanges) w.Quit()
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python實(shí)現(xiàn)kmp算法的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)kmp算法的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04詳解基于python的全局與局部序列比對(duì)的實(shí)現(xiàn)(DNA)
這篇文章主要介紹了詳解基于python的全局與局部序列比對(duì)的實(shí)現(xiàn)(DNA).文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)的代碼(JSON模塊)
這篇文章主要介紹了Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)的代碼(JSON模塊),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04