Python使用win32com.client的方法示例
更新時間:2023年02月19日 09:55:02 作者:robin2022
本文主要介紹了Python使用win32com.client的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
在網(wǎng)上搜索的時候,經(jīng)常看到兩種打開方式: 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使用range函數(shù)計算一組數(shù)和的方法
這篇文章主要介紹了python使用range函數(shù)計算一組數(shù)和的方法,涉及Python中range函數(shù)的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-05-05python抓取網(wǎng)頁內(nèi)容并進行語音播報的方法
今天小編就為大家分享一篇python抓取網(wǎng)頁內(nèi)容并進行語音播報的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12