#!/usr/bin/env python
# -*- coding: utf-8 -*-
import win32com.client
import os
#--------------------------------------------------------------------------
class easyWord:
'''
Some convenience methods for Excel documents accessed
through COM.一些方便的方法为Excel文档接入通过COM
'''
def __init__(self,visible=False):
self.wdApp = win32com.client.Dispatch('Word.Application')
self.wdApp.Visible = visible def new(self,filename=None):
'''
Create a new Word document. If 'filename' specified,
use the file as a template.创建一个新的word文件,
'''
if filename:
return self.wdApp.Documents.Add(filename)
else:
return self.wdApp.Documents.Add() def open(self,filename):
'''
Open an existing Word document for editing.
打开一个已经存在的word进行编辑
'''
return self.wdApp.Documents.Open(filename)
def visible(self,visible=True):
self.wdApp.Visible = visible def find(self,text,MatchWildcards=False):
'''
Find the string
查找字符串
'''
find = self.wdApp.Selection.Find
find.ClearFormatting()
find.Execute(text, False, False, MatchWildcards, False, False, True, 0)
return self.wdApp.Selection.Text
def replaceAll(self,oldStr,newStr):
'''
Find the oldStr and replace with the newStr.
替换所有字符串
'''
find = self.wdApp.Selection.Find
find.ClearFormatting()
find.Replacement.ClearFormatting()
find.Execute(oldStr, False, False, False, False, False, True, 1, True, newStr, 2)
def updateToc(self):
for tocitem in self.wdApp.ActiveDocument.TablesOfContents:
tocitem.Update()
def save(self):
'''
Save the active document
'''
self.wdApp.ActiveDocument.Save()
def saveAs(self,filename,delete_existing=True):
'''
Save the active document as a different filename.
If 'delete_existing' is specified and the file already
exists, it will be deleted before saving.
'''
if delete_existing and os.path.exists(filename):
os.remove(filename)
self.wdApp.ActiveDocument.SaveAs(FileName = filename)
def close(self):
'''
Close the active workbook.
'''
self.wdApp.ActiveDocument.Close()
def quit(self):
'''
Quit Word
'''
return self.wdApp.Quit()

最新文章

  1. 【转】XenServer架构之XAPI
  2. Groovy学习--基本语法了解
  3. Task set generation
  4. 【BZOJ1005】【HNOI2008】明明的烦恼
  5. mq_send
  6. 一些 PHP 管理系统程序中的后门
  7. [shell编程]正则表达式
  8. iOS 面试题:OC标题的基本概念<延续>
  9. 深入理解C指针之五:指针和字符串
  10. (转)spring事务管理几种方式
  11. CodeForces - 1101B
  12. 当前主流电脑的BIOS调出键
  13. java获取当前时间精确到毫秒
  14. postgresql 查询某一个表中的所有字段
  15. node(03)--利用 HTTP 模块 URl 模块 PATH 模块 FS 模块创建一个 WEB 服务器
  16. flutter学习地址
  17. Orchard Core 版本冲突 The type 'FormTagHelper' exists in both 'Microsoft.AspNetCore.Mvc.TagHelpers, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and...
  18. Maven的安装与本地仓库的搭建
  19. spark 创建稀疏向量和矩阵
  20. 快捷键&小技巧

热门文章

  1. HTML-Html开发之Viewport的使用
  2. asp.net 利用HttpWebRequest自动获取网页编码并获取网页源代码
  3. IOS一句话总结基础知识
  4. Intent的用法总结(不进你会后悔的)
  5. 算法笔记_147:有向图欧拉回路判断应用(Java)
  6. MyEclipse中出现Address already in use:JVM_Bind:8080
  7. 大话设计模式C++实现-第1章-简单工厂模式
  8. 【Linux】(2013-7-19)配置tftp与开发板传送文件
  9. javascript 的继承实例
  10. 为啥Android手机总会越用越慢?