pywin32處理Word和Excel的一些事
我們知道Python處理word和Excel的可以借助第三庫python-docx、xlrd、xlwt和openpyxl等實(shí)現(xiàn),但這些模塊只支持基本的讀寫操作,如果要實(shí)現(xiàn)一些較為深入功能,就要看模塊是否有相應(yīng)功能支持了。
例如將word和Excel轉(zhuǎn)為pdf,在word和Excel里面執(zhí)行VBA實(shí)現(xiàn)特殊功能,在Excel里面生成數(shù)據(jù)透視表、設(shè)置Excel的頁面設(shè)置等等。
除了使用上述模塊之外,還可以直接使用pywin32實(shí)現(xiàn)word和Excel的操作,其底層原理是通過win32com實(shí)現(xiàn)操作,不過pywin32目前只能在Windows上運(yùn)行,對(duì)linux和macos不太友好,并且電腦必須安裝wps或微軟office,這也是pywin32的一些局限。
pywin32將Word轉(zhuǎn)pdf
pywin32處理Word大多數(shù)用于格式轉(zhuǎn)換,因?yàn)橐话阕x寫操作都可以借助python-docx實(shí)現(xiàn),除非真的有特殊要求,但大部分企業(yè)對(duì)Wrod操作不會(huì)有太多復(fù)雜需求。
廢話不多說,直接上代碼
pythoncom.CoInitialize()
file_path = 'xxxx'
try:
word = wc.gencache.EnsureDispatch('word.application')
except:
try:
word = wc.gencache.EnsureDispatch('kwps.application') # 如果使用wps
except:
word = wc.gencache.EnsureDispatch('wps.application') # 如果使用wps
newpdf = word.Documents.Open(file_path)
word.Visible = 0
newpdf.SaveAs(f'xxxx.pdf', FileFormat=17)
newpdf.Close()
pythoncom.CoUninitialize()
使用pywin32創(chuàng)建Word對(duì)象可以使用微軟Excel(word.application)或Wps實(shí)現(xiàn)(kwps.application或wps.application),wps的kwps和wps主要是wps版本不同導(dǎo)致的,這個(gè)要看電腦安裝是那個(gè)版本的Wps。
使用win32com調(diào)用com組件的時(shí)候,需要用pythoncom.CoInitialize初始化一下,最后還需要用pythoncom.CoUninitialize釋放資源。
pywin32將Excel格式處理并轉(zhuǎn)pdf
由于pywin32處理Excel沒有相應(yīng)官方文檔說明,也沒有相應(yīng)源碼,那怎辦?實(shí)際上,官方文檔在微軟官方文檔有說明的,打開微軟官方文檔可以發(fā)現(xiàn),文檔是以VBA為主,換句話說,我們需要將VBA代碼轉(zhuǎn)換為Python代碼。
如果不太熟悉VBA代碼,可以打開Excel錄制宏代碼,錄制過程中將用戶操作Excel轉(zhuǎn)換為VBA代碼,如圖所示
關(guān)于Excel設(shè)置開發(fā)者工具,如何錄制VBA代碼這個(gè)自行百度一下了。
然后從VBA代碼在官方文檔找到對(duì)應(yīng)屬性,比如VBA代碼的PageSetup是Excel的頁面設(shè)置,在官方文檔找到PageSetup相關(guān)屬性方法,
按照上述,VBA轉(zhuǎn)換對(duì)應(yīng)Python如下:
from win32com import client as wc
try:
excel = wc.DispatchEx('Excel.Application')
except:
try:
excel = wc.DispatchEx('ket.Application')
except:
excel = wc.DispatchEx('et.Application')
newpdf = excel.Workbooks.Open(r'C:\Users\Administrator\Desktop\tt\abc.xls')
excel.DisplayAlerts = 0
# 獲取第一個(gè)sheet
all_sheets = [sheet.Name for sheet in newpdf.Sheets]
ws_source = newpdf.Worksheets(all_sheets[0])
# 設(shè)置頁面設(shè)置
ws_source.PageSetup.LeftHeader = ""
ws_source.PageSetup.CenterHeader = ""
ws_source.PageSetup.RightHeader = ""
ws_source.PageSetup.LeftFooter = ""
ws_source.PageSetup.CenterFooter = ""
ws_source.PageSetup.RightFooter = ""
# ws_source.PageSetup.FitToPagesTall = 0
ws_source.PageSetup.FirstPageNumber = True
ws_source.PageSetup.LeftMargin = 0
ws_source.PageSetup.RightMargin = 0
ws_source.PageSetup.TopMargin = 0
ws_source.PageSetup.BottomMargin = 0
ws_source.PageSetup.HeaderMargin = 0
ws_source.PageSetup.FooterMargin = 0
# ws_source.PageSetup.PaperSize = 1
ws_source.PageSetup.Orientation = 2 # 橫向轉(zhuǎn)換pdf
ws_source.PageSetup.FitToPagesWide = 1 # 所有列壓縮在一頁紙
ws_source.PageSetup.FitToPagesTall = False
ws_source.PageSetup.Zoom = False # 所有列壓縮在一頁紙
ws_source.PageSetup.CenterVertically = True
ws_source.PageSetup.CenterHorizontally = True
ws_source.PageSetup.Draft = False
ws_source.Select()
# 行列自動(dòng)調(diào)整
# ws_source.Columns.AutoFit()
# ws_source.Rows.AutoFit()
# 設(shè)置Excel的邊框
rows = ws_source.UsedRange.Rows.Count
cols = ws_source.UsedRange.Columns.Count
ws_source.Range(ws_source.Cells(1, 1), ws_source.Cells(rows, cols)).Borders.LineStyle = 1
ws_source.Range(ws_source.Cells(1, 1), ws_source.Cells(rows, cols)).Borders.TintAndShade = 0
ws_source.Range(ws_source.Cells(1, 1), ws_source.Cells(rows, cols)).Borders.Weight = 1
# 轉(zhuǎn)換為PDF文件
newpdf.ExportAsFixedFormat(0, r'C:\Users\Administrator\Desktop\tt\abc.pdf')
newpdf.Close()
excel.Quit()
從Python代碼看到,它與VBA代碼十分相似,也就是說,VBA能實(shí)現(xiàn)的功能,Python也能實(shí)現(xiàn),說明了,編程語言只是工具,不管是什么工具,只要玩的溜就行了。文章來源:http://www.zghlxwxcb.cn/news/detail-445078.html
總結(jié)
從微軟官方文檔看到,VBA還支持office所有軟件,如Access、outlook、ppt等等開發(fā),那么Python使用pywin32也能實(shí)現(xiàn)這些開發(fā)。不管如此,還可以實(shí)現(xiàn)Windows其他功能開發(fā),如自動(dòng)打印功能使用win32print,這都是pywin32模塊。
更多內(nèi)容請(qǐng)關(guān)注公眾號(hào)文章來源地址http://www.zghlxwxcb.cn/news/detail-445078.html
到了這里,關(guān)于Python pywin32實(shí)現(xiàn)word和Excel的處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!