pywin32处理Word和Excel的一些事
我们知道Python处理word和Excel的可以借助第三库python-docx、xlrd、xlwt和openpyxl等实现,但这些模块只支持基本的读写操作,如果要实现一些较为深入功能,就要看模块是否有相应功能支持了。
例如将word和Excel转为pdf,在word和Excel里面执行VBA实现特殊功能,在Excel里面生成数据透视表、设置Excel的页面设置等等。
除了使用上述模块之外,还可以直接使用pywin32实现word和Excel的操作,其底层原理是通过win32com实现操作,不过pywin32目前只能在Windows上运行,对linux和macos不太友好,并且电脑必须安装wps或微软office,这也是pywin32的一些局限。
pywin32将Word转pdf
pywin32处理Word大多数用于格式转换,因为一般读写操作都可以借助python-docx实现,除非真的有特殊要求,但大部分企业对Wrod操作不会有太多复杂需求。
废话不多说,直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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创建Word对象可以使用微软Excel(word.application)或Wps实现(kwps.application或wps.application),wps的kwps和wps主要是wps版本不同导致的,这个要看电脑安装是那个版本的Wps。
使用win32com调用com组件的时候,需要用pythoncom.CoInitialize初始化一下,最后还需要用pythoncom.CoUninitialize释放资源。
pywin32将Excel格式处理并转pdf
由于pywin32处理Excel没有相应官方文档说明,也没有相应源码,那怎办?实际上,官方文档在微软官方文档有说明的,打开微软官方文档可以发现,文档是以VBA为主,换句话说,我们需要将VBA代码转换为Python代码。
如果不太熟悉VBA代码,可以打开Excel录制宏代码,录制过程中将用户操作Excel转换为VBA代码,
如图所示:
关于Excel设置开发者工具,如何录制VBA代码这个自行百度一下了。
然后从VBA代码在官方文档找到对应属性,比如VBA代码的PageSetup是Excel的页面设置,在官方文档找到PageSetup相关属性方法,
按照上述,VBA转换对应Python如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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 # 获取第一个sheet all_sheets = [sheet.Name for sheet in newpdf.Sheets] ws_source = newpdf.Worksheets(all_sheets[ 0 ]) # 设置页面设置 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 # 横向转换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() # 行列自动调整 # ws_source.Columns.AutoFit() # ws_source.Rows.AutoFit() # 设置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 # 转换为PDF文件 newpdf.ExportAsFixedFormat( 0 , r 'C:\Users\Administrator\Desktop\tt\abc.pdf' ) newpdf.Close() excel.Quit() |
从Python代码看到,它与VBA代码十分相似,也就是说,VBA能实现的功能,Python也能实现,说明了,编程语言只是工具,不管是什么工具,只要玩的溜就行了。
总结
从微软官方文档看到,VBA还支持office所有软件,如Access、outlook、ppt等等开发,那么Python使用pywin32也能实现这些开发。不管如此,还可以实现Windows其他功能开发,如自动打印功能使用win32print,这都是pywin32模块。
到此这篇关于Python pywin32实现word与Excel的处理的文章就介绍到这了,更多相关Python word和Excel处理内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/HuangZhang_123/article/details/126401212