Python使用win32com.client的方法示例
作者:robin2022 时间:2021-03-22 14:32:48
在网上搜索的时候,经常看到两种打开方式: 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
#创建
#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() #创建新的文档
#excel
xlBook = xlApp.Workbooks.Open(FileName)
#new_xlBook = xlApp.Workbooks.Add() #创建新的工作簿
#插入文字
#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
#设置
_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))
#转换为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()
#关闭
#word
#doc.Close()
w.Documents.Close(wc.wdDoNotSaveChanges)
w.Quit()
#excel
xlBook.Close(SaveChange = 0)
xlBook.Quit()
来源:https://www.cnblogs.com/robin2022/p/16317499.html
标签:Python,win32com.client
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在python中读取和写入CSV文件详情
2021-01-21 22:34:51
![](https://img.aspxhome.com/file/2023/5/65525_0s.png)
SqlServer 巧妙解决多条件组合查询
2023-07-01 14:15:00
如何判断电子邮件的地址格式是否正确?
2010-01-12 20:12:00
解决pycharm中导入自己写的.py函数出错问题
2023-07-09 12:12:05
![](https://img.aspxhome.com/file/2023/2/60302_0s.jpg)
javascript new fun的执行过程
2010-08-05 21:23:00
![](https://img.aspxhome.com/file/UploadPic/20108/5/20100805150736846-93s.jpg)
python实现二级登陆菜单及安装过程
2023-09-16 16:05:44
![](https://img.aspxhome.com/file/2023/5/65955_0s.jpg)
重新编译PLSQL中的无效对象或者指定的对象 的方法
2009-02-26 10:41:00
Adobe发布Flash Player 10正式版
2008-10-15 17:15:00
Python常用小技巧总结
2023-02-27 17:50:16
讲解数据库管理系统必须提供的基本服务
2009-01-04 14:33:00
Oracle对两个数据表交集的查询
2010-07-26 12:51:00
解决golang编译提示dial tcp 172.217.160.113:443: connectex: A connection attempt failed(推荐)
2023-07-16 04:24:49
Python中的TCP socket写法示例
2023-06-25 00:21:05
![](https://img.aspxhome.com/file/2023/7/60707_0s.jpg)
Python使用tkinter实现小时钟效果
2022-08-14 09:00:18
![](https://img.aspxhome.com/file/2023/8/68888_0s.jpg)
asp阻止中国ip访问访问
2011-09-13 12:55:37
Linux+php+apache+oracle环境搭建之CentOS下安装Oracle数据库
2023-10-08 01:02:56
Tensorflow 实现线性回归模型的示例代码
2023-07-03 09:00:41
![](https://img.aspxhome.com/file/2023/6/61166_0s.jpg)
asp 动态生成rss(不成生xml文件)代码
2011-04-04 11:17:00
Python正规则表达式学习指南
2021-04-11 15:21:16
![](https://img.aspxhome.com/file/2023/7/68827_0s.png)
轻松掌握如何从命令行启动mysqld服务器
2008-12-31 15:47:00