python模块win32com用法详解
更新时间:2024-07-12 02:57:01 阅读量: 综合文库 文档下载
- Python模块的本质推荐度:
- 相关推荐
python模块:win32com用法详解
使用技巧
import win32com
from win32com.client import Dispatch, constants w = win32com.client.Dispatch('Word.Application') # 或者使用下面的方法,使用启动独立的进程:
# w = win32com.client.DispatchEx('Word.Application') # 后台运行,不显示,不警告 w.Visible = 0 w.DisplayAlerts = 0 # 打开新的文件
doc = w.Documents.Open( FileName = filenamein ) # worddoc = w.Documents.Add() # 创建新的文档 # 插入文字
myRange = doc.Range(0,0)
myRange.InsertBefore('Hello from Python!') # 使用样式
wordSel = myRange.Select()
wordSel.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) # 页眉文字替换
w.ActiveDocument.Sections[0].Headers[0].Range.Find.ClearFormatting()
w.ActiveDocument.Sections[0].Headers[0].Range.Find.Replacement.ClearFormatting()
w.ActiveDocument.Sections[0].Headers[0].Range.Find.Execute(OldStr, False, False, False, False, False, True, 1, False, NewStr, 2) # 表格操作
doc.Tables[0].Rows[0].Cells[0].Range.Text ='123123' worddoc.Tables[0].Rows.Add() # 增加一行 # 转换为html
wc = win32com.client.constants
w.ActiveDocument.WebOptions.RelyOnCSS = 1
w.ActiveDocument.WebOptions.OptimizeForBrowser = 1 w.ActiveDocument.WebOptions.BrowserLevel 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 = filenameout, FileFormat = wc.wdFormatHTML ) # 打印 doc.PrintOut() # 关闭 # doc.Close()
w.Documents.Close(wc.wdDoNotSaveChanges) w.Quit()
=
0
#
(3)处理excel
[1]使用PyExcelerator读写EXCEL
文件(Platform: Win,Unix-like)
优点:简单易
用 缺点:不可改变已存在的EXCEL文件。
文件处理python
包。它主要是
PyExcelerator是一个开源的MS Excel
用来写 Excel 文件.URL:
http://sourceforge.net/projects/pyexcelerator/
http://blog.donews.com/limodou/archive/2005/07/09/460033.aspx
我没有找到关于PyExcelerator的文档。只是看到了limodou的一篇介绍。
这个包使用起来还是比较简单的:)。带了很多小例子,可以参照。 例mini.py.
python
=================================
#!/usr/bin/env
# -*- coding: windows-1251 -*-
# Copyright (C) 2005 Kiseliov Roman
__rev_id__ = \\导入模块
from pyExcelerator import * \生成一个工作薄
w = Workbook() \加入一个Sheet
ws = w.add_sheet('Hey, Dude') \保存
文件(VBAXL.CHM)。这里面讲述了
w.save('mini.xls')
================================= [2]使用COM接口,直接操作EXCEL(只能在Win上) 优点:可以满足绝大数要求。缺点:有些麻烦。:-)
这方面的例子很多,GOOGLE 看吧:-). 文档也可以参看OFFICE自带的VBA EXCEL 帮助EXCEL VBA的编程概念,
EXCEL
不错的教程!另外,《
Python Programming on Win32》书中也有很详细的介绍。这本书中给出了一个类来操作文
件,可以很容易的加以扩展。
#!/usr/bin/env
python
# -*- coding: utf-8 -*-
from win32com.client import Dispatch import win32com.client class easyExcel:
\ Remembering to save the data is your problem, as is error handling. Operates on one workbook at a time.\ def __init__(self, filename=None):
self.xlApp = win32com.client.Dispatch('Excel.Application') if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename) else:
self.xlBook = self.xlApp.Workbooks.Add() self.filename = ''
def save(self, newfilename=None):
if newfilename:
self.filename = newfilename
self.xlBook.SaveAs(newfilename) else:
self.xlBook.Save() def close(self):
self.xlBook.Close(SaveChanges=0) del self.xlApp
def getCell(self, sheet, row, col): \
sht = self.xlBook.Worksheets(sheet) return sht.Cells(row, col).Value
def setCell(self, sheet, row, col, value): \
sht = self.xlBook.Worksheets(sheet) sht.Cells(row, col).Value = value
def getRange(self, sheet, row1, col1, row2, col2): \ sht = self.xlBook.Worksheets(sheet)
return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value def addPicture(self, sheet, pictureName, Left, Top, Width, Height): \
sht = self.xlBook.Worksheets(sheet)
sht.Shapes.AddPicture(pictureName, 1, 1, Left, Top, Width, Height) def cpSheet(self, before): \
shts = self.xlBook.Worksheets shts(1).Copy(None,shts(1)) \下面是一些测试代码。
if __name__ == \
xls = easyExcel(r'D:\\test.xls')
PNFILE = r'c:\\screenshot.bmp'
xls.addPicture('Sheet1', PNFILE, 20,20,1000,1000) xls.cpSheet('Sheet1')
xls.close()
xls.save()
(4)python调用短信猫控件,发短信
#! /usr/bin/env python #coding=gbk
import sys
import win32com.client
ocxname='ShouYan_SmsGate61.Smsgate' axocx=win32com.client.Dispatch(ocxname) axocx.CommPort=8#设置COM端口号
axocx.SmsService='+8613800100500'#设置短信服务号码 axocx.Settings='9600,n,8,1'#设置com端口速度 axocx.sn='loyin'
c=axocx.Connect(1)#连接短信猫或手机
print '连接情况',axocx.Link()
axocx.SendSms('python确实是很好的','15101021000',0)#发送短信
import sys
import win32com.client
ocxname='ShouYan_SmsGate61.Smsgate' axocx=win32com.client.Dispatch(ocxname) axocx.CommPort=8#设置COM端口号
axocx.SmsService='+8613800100500'#设置短信服务号码 axocx.Settings='9600,n,8,1'#设置com端口速度 axocx.sn='loyin'
c=axocx.Connect(1)#连接短信猫或手机
print '连接情况',axocx.Link()
axocx.SendSms('python确实是很好的','15101021000',0)#发送短信
正在阅读:
python模块win32com用法详解07-12
人工神经网络复习题06-20
快乐的女战士教学设计05-19
娃哈哈锌AD钙奶市场营销策划方案04-16
链霉素的生产10-28
赴南京化工机械厂生产实习报告01-06
新年晚会致辞02-20
机电一体化系统设计05-08
矛盾论读后感11-27
领导为官不为整改措施报告简易版05-07
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 用法
- 详解
- 模块
- win32com
- python