python操作word【简单封装】
2024-08-31 22:12:02
#!/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()
最新文章
- 【转】XenServer架构之XAPI
- Groovy学习--基本语法了解
- Task set generation
- 【BZOJ1005】【HNOI2008】明明的烦恼
- mq_send
- 一些 PHP 管理系统程序中的后门
- [shell编程]正则表达式
- iOS 面试题:OC标题的基本概念&;lt;延续&;gt;
- 深入理解C指针之五:指针和字符串
- (转)spring事务管理几种方式
- CodeForces - 1101B
- 当前主流电脑的BIOS调出键
- java获取当前时间精确到毫秒
- postgresql 查询某一个表中的所有字段
- node(03)--利用 HTTP 模块 URl 模块 PATH 模块 FS 模块创建一个 WEB 服务器
- flutter学习地址
- Orchard Core 版本冲突 The type 'FormTagHelper' exists in both 'Microsoft.AspNetCore.Mvc.TagHelpers, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and...
- Maven的安装与本地仓库的搭建
- spark 创建稀疏向量和矩阵
- 快捷键&;小技巧
热门文章
- HTML-Html开发之Viewport的使用
- asp.net 利用HttpWebRequest自动获取网页编码并获取网页源代码
- IOS一句话总结基础知识
- Intent的用法总结(不进你会后悔的)
- 算法笔记_147:有向图欧拉回路判断应用(Java)
- MyEclipse中出现Address already in use:JVM_Bind:8080
- 大话设计模式C++实现-第1章-简单工厂模式
- 【Linux】(2013-7-19)配置tftp与开发板传送文件
- javascript 的继承实例
- 为啥Android手机总会越用越慢?