欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用win32com.client的方法示例

 更新時(shí)間:2023年02月19日 09:55:02   作者:robin2022  
本文主要介紹了Python使用win32com.client的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

在網(wǎng)上搜索的時(shí)候,經(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")

兩種方式的差別參見:

https://stackoverflow.com/questions/50127959/win32-dispatch-vs-win32-gencache-in-python-what-are-the-pros-and-cons

#創(chuàng)建

#word
w = win32com.client.Dispatch("Word.Application") ? ?
w = win32com.client.DispatchEx("Word.Application")#使用啟動(dòng)獨(dú)立的進(jìn)程

#excel
xlApp = win32com.client.Dispatch("Excel.Application")

#后臺(tái)運(yùn)行, 不顯示, 不警告
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論