python Word 编程
来源:互联网 发布:在线模拟装机软件 编辑:程序博客网 时间:2024/05/16 23:44
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 = 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 = filenameout, FileFormat = wc.wdFormatHTML )
# 打印
doc.PrintOut()
# 关闭
# doc.Close()
w.Documents.Close(wc.wdDoNotSaveChanges)
w.Quit()
- python Word 编程
- Python office编程:word和PowerPoint
- Python word
- python:word
- Python操作word
- python操作Excel word
- Python操作Word
- python word count
- Word Cloud Python
- python 操作word ecel
- python 操作Word文档
- python 处理 word
- python读取word
- 用Python读写word
- python操作word docx
- python word读取
- RabbitMQ hello word (python)
- python的Hello Word
- 一个很好玩的jquery例子:小方块先逐渐显示,后向右滑动,再向左滑回,最后向上隐藏
- mongodb注册为服务
- django1.2.4 session保存有bug
- python数组交集、并集与不同
- python数组交集、并集与不同
- python Word 编程
- eclipse新建python项Project interpreter not specified
- python 调用word打开doc文档报错,“word无法启动转换器mswrd632.wpc”
- 使用Python和xml.etree.ElementTree解析xml文件
- mysql中limit的查询优化,千万级也能轻松搞定
- python的xml库操作读写文件真是慢的无语,跟直接操作文本型的操作真不是一个级别的
- 替换xml中的非法字符及不可见字符
- linux中强大的screen命令
- 用Python生成不依赖dll的exe的操作步骤简介