#!/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()

python操作word【简单封装】的更多相关文章

  1. python操作word(改课文格式)【最终版】

    python操作word的一些方法,前面写了一些感悟,有点跑题,改了下题目,方便能搜索到.心急的可以直接拉到最后看代码,我都加了比较详细的注释. 从8.3号早上9点,到8.8号下午5点半下班,终于把这 ...

  2. 利用Python操作Word文档【图片】

    利用Python操作Word文档

  3. Python操作Word:常用对象介绍

    前面已经介绍过了试用win32com类库来进行Word开发,系列文章<Python操作Word>是继承了前面的文章,所以,你应该先查看前面的文章,其实只有两篇,文章地址列在最下面的参考资料 ...

  4. python操作word

    python教程(百度经验) Python 操作Word(Excel.PPT等通用)   import win32comfrom win32com.client import Dispatch, co ...

  5. python操作word入门

    1.安装pywin32 http://sourceforge.net/projects/pywin32 在files里去找适合你的python版本.截止此文,最新版本是pywin32-219快捷路径: ...

  6. python操作word、ppt的详解

    python使用win32com的心得   python可以使用一个第三方库叫做win32com达到操作com的目的, 我是安装了ActivePython的第三方库,从官网下载了安装包,该第三方库几乎 ...

  7. Python操作Word【批量生成文章】

    http://www.cnblogs.com/codex/p/4668396.html 需要做一些会议记录.总共有多少呢?五个地点x7个月份x每月4篇=140篇.虽然不很重要,但是140篇记录完全雷同 ...

  8. Python操作Word批量生成文章

    需要做一些会议记录.总共有多少呢?五个地点x7个月份x每月4篇=140篇.虽然不很重要,但是140篇记录完全雷同也不好.大体看了一下,此类的记录大致分为四段.于是决定每段提供四种选项,每段从四选项里随 ...

  9. Python操作Word与Excel并打包

    安装模块 # Word操作库 pip install docx # Excel操作库 pip install openpyxl # 打包exe工具 pip install pyinstaller Wo ...

随机推荐

  1. 倾斜摄影三维建模软件photoscan教程 [转]

    PhotoScan是一款基于影响自动生成高质量三维模型的优秀软件,这对于3D建模需求来说实在是一把利器. PhotoScan无需设置初始值,无须相机检校,它根据最新的多视图三维重建技术,可对任意照片进 ...

  2. 给出a的定义 -- 指针 和 数组

  3. UVALive 4857 Halloween Costumes

    区间dp.对于最左边的点: 1.在该点穿的衣服只有该点用的到,即穿上就脱下.所以dp[ l ][ r ] = min(dp[ l + 1][ r ] + 1, dp[ l ][ r ]). 2.衣服仍 ...

  4. RootMotionComputer 根运动计算机

    using UnityEngine; using System.Collections; /* * -------------------------------------------------- ...

  5. sae python中Mysql中文乱码的解决

    一開始我用的是: db=MySQLdb.connect(db=sae.const.MYSQL_DB,user=sae.const.MYSQL_USER,passwd=sae.const.MYSQL_P ...

  6. chrome 设置启动时打开特定一组网页

      chrome 设置启动时打开特定一组网页 CreateTime--2018年4月25日08:57:00 Author:Marydon 1.使用场景 经常有一些必用的网站,每天打开chrome都要依 ...

  7. 〖C++〗string2int把字符串转换成int的函数

    #include <stdio.h> #include <stdlib.h> #include <string.h> int string2int(char *ar ...

  8. Web中路径问题

    如果在web项目中需要使用路径,如:转发.重定向还有超链接等. 原则:”一切web路径以/开始” 那么/的路径一定是相对路径,那么/到底代表哪一个相对路径是需要智慧的. 如果该路径是给服务器使用的,那 ...

  9. 【LeetCode】97. Interleaving String

    Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Fo ...

  10. Linux环境源码编译安装SVN

    zhoulf 2015/2/28 原创 安装说明 安装环境:Red Hat Enterprise Linux 安装方式:源码安装 软件:apr-1.5.0.tar.gz.apr-iconv-1.2.1 ...