Python使用win32com.client的方法示例
更新時間:2023年02月19日 09:55:02 作者:robin2022
本文主要介紹了Python使用win32com.client的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
在網(wǎng)上搜索的時候,經(jīng)??吹絻煞N打開方式: dispatch和EnsureDispatch
import win32com.client as win32 xl_dis = win32.Dispatch("Excel.Application")
import win32com.client as win32 xl_ens = win32.gencache.EnsureDispatch("Excel.Application")
兩種方式的差別參見:
#創(chuàng)建 #word w = win32com.client.Dispatch("Word.Application") ? ? w = win32com.client.DispatchEx("Word.Application")#使用啟動獨立的進程 #excel xlApp = win32com.client.Dispatch("Excel.Application") #后臺運行, 不顯示, 不警告 w.Visible = 0; w.DisplayAlerts = 0; #打開新的文件 #word doc = w.Documents.Open(FileName) #new_doc = w.Documents.Add() #創(chuàng)建新的文檔 #excel xlBook = xlApp.Workbooks.Open(FileName) #new_xlBook = xlApp.Workbooks.Add() #創(chuàng)建新的工作簿 #插入文字 #word myRange = doc.Range(0, 0) myRange.InsertBefore("hello from Python") #excel #使用樣式 wordStyle = myRange.Select() wordStyle.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) #表格操作 #word doc.Tables[0].Rows[0].Cells[0].Range.Text = "hello world Python" worddoc.Tables[0].Rows.Add() #增加一行 #excel #獲取 _sheet = xlBook.Worksheets(sheet) _sheet.Cell(row, col).Value #設(shè)置 _sheet = xlBook.Worksheets(sheet) _sheet.Cells(row, col).Value = values #范圍操作 _sheet = xlBook.Worksheets(sheet) _sheet.Range(_sheet.Cell(row1, col1), _sheet.Cell(row2, col2)).Value #添加圖片 #excel _sheet = xlBook.Worksheets(sheet) _sheet.Shapes.AddPicture(picturename, 1, 1, Left, Top, Width, Height) #copy 工作簿 sheets = xlBook.Worksheets sheets(1).Copy(None, sheets(1)) #轉(zhuǎn)換為html #word 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, FileFormat = wc.wdFormatHTML) #打印 doc.PrintOut() #保存 #excel xlBook.SaveAs(FileName)#另存為 xlBook.Save() #關(guān)閉 #word #doc.Close() w.Documents.Close(wc.wdDoNotSaveChanges) w.Quit() #excel xlBook.Close(SaveChange = 0) xlBook.Quit()
到此這篇關(guān)于Python使用win32com.client的方法示例的文章就介紹到這了,更多相關(guān)Python使用win32com.client內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python NumPy數(shù)組裁切和數(shù)據(jù)類型的實現(xiàn)即原理詳解
這篇文章主要介紹了Python NumPy數(shù)組裁切和數(shù)據(jù)類型的實現(xiàn)即原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-05-05python 解析XML python模塊xml.dom解析xml實例代碼
這篇文章主要介紹了分享下python中使用模塊xml.dom解析xml文件的實例代碼,學習下python解析xml文件的方法,有需要的朋友參考下2014-02-02關(guān)于pip install uwsgi安裝失敗問題的解決方案
這篇文章主要介紹了關(guān)于pip install uwsgi安裝失敗問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06Python入門教程(三十九)Python的NumPy安裝與入門
這篇文章主要介紹了Python入門教程(三十九)Python的NumPy安裝與入門,NumPy 是一個Python包,它是一個由多維數(shù)組對象和用于處理數(shù)組的例程集合組成的庫,,需要的朋友可以參考下2023-05-05