Python中win32com模塊的使用
0 前言
安裝:
pip install pypiwin32
1 Excel的API
import win32com.client as win32 #創(chuàng)建 xlApp = win32.Dispatch("Excel.Application") xlApp = win32.DispatchEx("Excel.Application")#使用啟動(dòng)獨(dú)立的進(jìn)程 #后臺(tái)運(yùn)行, 不顯示, 不警告 xlApp .Visible = 0; xlApp DisplayAlerts = 0; #打開(kāi)新的文件 xlBook = xlApp.Workbooks.Open(FileName) #創(chuàng)建新的工作簿 new_xlBook = xlApp.Workbooks.Add() #獲取 xlSheet = xlBook.Worksheets('Sheet1') a = xlSheet .Cell(11, 5).Value ?# (row, col) 都是從1開(kāi)始 xlSheet .Cells(11, 5).Value = 2 ?# (row, col) 都是從1開(kāi)始 #范圍操作 xlSheet.Range(xlSheet.Cell(11, 5), xlSheet.Cell(13, 6)).Value #添加圖片 xlSheet.Shapes.AddPicture(picturename, 1, 1, Left, Top, Width, Height) #copy 工作簿 xlSheet2.Copy(None, xlSheet) #保存 xlBook.SaveAs(FileName)#另存為 xlBook.Save() #退出 xlBook.Close() xlBook.Quit()
1.1 Excel示例
#!/usr/bin/env python? # -*- coding:utf-8 -*- import win32com.client as win32 if __name__ == "__main__": ? ? ''' ? ?啟動(dòng)Excel程序并打開(kāi)文件 ? ''' ? ? app = win32.DispatchEx('Excel.Application') # 啟動(dòng)獨(dú)立的進(jìn)程,關(guān)閉時(shí)不影響其他文件 ? ? app.Visible = False ? # 不打開(kāi)excel界面 ? ? app.DisplayAlerts = False ? # 不顯示警告信息 ? ? wb = app.Workbooks.Open(r'D:\6_SoftwareTest\xml\empty_book.xlsx') ? ? # wb = app.Workbooks.Add() ?#創(chuàng)建新文件 ? ? ''' ? ?處理worksheet ? ''' ? ? ws = wb.Worksheets('Sheet1') ? ? ws.Cells(11, 5).Value = 2 ?# Cells(row,col) 先行后列 Cells(11,5)就指的是 E11單元格 ? ? ws.Cells(11, 5).offset(3, 2).Value = 1 ?# E11 偏移后 到了 F13,移動(dòng)(3-1,2-1) ? ? ws.Range('D10').value = 15 ?# 這里指對(duì)D10寫(xiě)入數(shù)據(jù) 15 ? ? ws1 = wb.Worksheets ? ? ws1.Copy(None, ws) ? ? ''' ? ?保存并關(guān)閉Excel文件,退出程序 ? ''' ? ? wb.Save() ? ? # wb.SaveAs(path) ? ? wb.Close() ? ? app.Quit()
2 Word的API
import win32com.client as win32 #創(chuàng)建 wdApp = win32.Dispatch("Word.Application") ? ? wdApp = win32.DispatchEx("Word.Application")#使用啟動(dòng)獨(dú)立的進(jìn)程 #后臺(tái)運(yùn)行, 不顯示, 不警告 wdApp.Visible = 0; wdApp.DisplayAlerts = 0; #打開(kāi)新的文件 doc = wdApp.Documents.Open(FileName) #創(chuàng)建新的文檔 new_doc = wdApp.Documents.Add() #插入文字 myRange = doc.Range(0, 0) myRange.InsertBefore("hello from Python") #使用樣式 wordStyle = myRange.Select() wordStyle.Style = constants.wdStyleHeading1 #正文文字替換 wdApp.Selection.Find.ClearFormatting() wdApp.Selection.Find.Replacement.ClearFormatting() wdApp.Selection.Find.Execute(OldStr, False, False, False, False, False, True, 1, True, NewStr, 2) #表格操作 doc.Tables[0].Rows[0].Cells[0].Range.Text = "hello world Python" doc.Tables[0].Rows.Add() #增加一行 #轉(zhuǎn)換為html wc = win32.constants wdApp.ActiveDocument.WebOptions.RelyOnCSS = 1 wdApp.ActiveDocument.WebOptions.OptimizeForBrowser = 1 wdApp.ActiveDocument.WebOptions.BrowserLevel = 0 # constants.wdBrowserLevelV4 wdApp.ActiveDocument.WebOptions.OrganizeInFolder = 0 wdApp.ActiveDocument.WebOptions.UseLongFileNames = 1 wdApp.ActiveDocument.WebOptions.RelyOnVML = 0 wdApp.ActiveDocument.WebOptions.AllowPNG = 1 wdApp.ActiveDocument.SaveAs(FileName, FileFormat = wc.wdFormatHTML) #打印 doc.PrintOut() #保存 doc.SaveAs(FileName)#另存為 doc.Save() #關(guān)閉 doc.Close() wdApp.Quit()
到此這篇關(guān)于Python中win32com模塊的使用的文章就介紹到這了,更多相關(guān)Python win32com模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Python從全局與局部變量到裝飾器的相關(guān)知識(shí)
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著Python從全局與局部變量到裝飾器的相關(guān)知識(shí)展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06全解析MeanShift傳統(tǒng)目標(biāo)跟蹤算法
meanshift算法的原理很簡(jiǎn)單,假設(shè)你有一堆點(diǎn)集,還有一個(gè)小的窗口,這個(gè)窗口可能是圓形的,現(xiàn)在你可能要移動(dòng)這個(gè)窗口到點(diǎn)集密度最大的區(qū)域當(dāng)中,這篇文章主要介紹了傳統(tǒng)目標(biāo)跟蹤——MeanShift算法,需要的朋友可以參考下2025-05-05python利用sklearn包編寫(xiě)決策樹(shù)源代碼
這篇文章主要為大家詳細(xì)介紹了python利用sklearn包編寫(xiě)決策樹(shù)源代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Python基于Opencv來(lái)快速實(shí)現(xiàn)人臉識(shí)別過(guò)程詳解(完整版)
這篇文章主要介紹了Python基于Opencv來(lái)快速實(shí)現(xiàn)人臉識(shí)別過(guò)程詳解(完整版)隨著人工智能的日益火熱,計(jì)算機(jī)視覺(jué)領(lǐng)域發(fā)展迅速,今天就為大家?guī)?lái)最基礎(chǔ)的人臉識(shí)別基礎(chǔ),從一個(gè)個(gè)函數(shù)開(kāi)始走進(jìn)這個(gè)奧妙的世界,需要的朋友可以參考下2019-07-07python爬蟲(chóng)把url鏈接編碼成gbk2312格式過(guò)程解析
這篇文章主要介紹了python爬蟲(chóng)把url鏈接編碼成gbk2312格式過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06分析Python中設(shè)計(jì)模式之Decorator裝飾器模式的要點(diǎn)
這篇文章主要介紹了Python中設(shè)計(jì)模式之Decorator裝飾器模式模式,文中詳細(xì)地講解了裝飾對(duì)象的相關(guān)加鎖問(wèn)題,需要的朋友可以參考下2016-03-03