python操作word文档表格
office 2007中不能直接打开VB编辑器,请按Alt + F11打开。
import win32com.client # 导入脚本模块 WordApp = win32com.client.Dispatch("Word.Application") # 载入WORD模块 WordApp.Visible = True # 显示Word应用程序 1、 新建Word文档 doc = WordApp.Documents.Add() # 新建空文件 doc = WordApp.Documents.Open(r"d:\2011专业考试计划.doc") # 打开指定文档 doc.SaveAs(r"d:\2011专业考试计划.doc") # 文档保存 doc.Close(-1) # 保存后关闭,doc.Close()或doc.Close(0)直接关闭不保存 2、 页面设置 doc.PageSetup.PaperSize = 7 # 纸张大小, A3=6, A4=7 doc.PageSetup.PageWidth = 21*28.35 # 直接设置纸张大小, 使用该设置后PaperSize设置取消 doc.PageSetup.PageHeight = 29.7*28.35 # 直接设置纸张大小 doc.PageSetup.Orientation = 1 # 页面方向, 竖直=0, 水平=1 doc.PageSetup.TopMargin = 3*28.35 # 页边距上=3cm,1cm=28.35pt doc.PageSetup.BottomMargin = 3*28.35 # 页边距下=3cm doc.PageSetup.LeftMargin = 2.5*28.35 # 页边距左=2.5cm doc.PageSetup.RightMargin = 2.5*28.35 # 页边距右=2.5cm doc.PageSetup.TextColumns.SetCount(2) # 设置页面分栏=2 3、 格式设置 sel = WordApp.Selection # 获取Selection对象 sel.InsertBreak(8) # 插入分栏符=8, 分页符=7 sel.Font.Name = "黑体" # 字体 sel.Font.Size = 24 # 字大 sel.Font.Bold = True # 粗体 sel.Font.Italic = True # 斜体 sel.Font.Underline = True # 下划线 sel.ParagraphFormat.LineSpacing = 2*12 # 设置行距,1行=12磅 sel.ParagraphFormat.Alignment = 1 # 段落对齐,0=左对齐,1=居中,2=右对齐 sel.TypeText("XXXX") # 插入文字 sel.TypeParagraph() # 插入空行 注:ParagraphFormat属性必须使用TypeParagraph()之后才能二次生效
python操作word文档,如何合并单元格
>>>app=my.Office.Word.GetInstance()>>>doc=app.Documents[0]>>>table=doc.Tables[1]>>>table.Cell(1,1).Select()>>>app.Selection.MoveDown(Unit=5, Count=2, Extend=1)>>>app.Selection.Cells.Merge()>>>1. my.Office.Word.GetInstance()用win32com得到Word的Application对象的实例2. 我所使用的样本word文件中包含两个Table第二个Table是想要修改的3. table.Cell(1,1).Select()用于选中这个样表的第一个单元格4. app.Selection.MoveDown用于获得向下多选取3个单元格5. app.Selection.Cells.Merge()用于执行合并工作
关于python 操作WORD文档 doc.Paragraphs[3].Range.Style.Font.Color...
word中doc这个格式的文件是微软特有格式,微软没有向外公开任何的api接口文档,只能通过微软提供的OLE组件来提其COM接口,只要你的机器上安装了Offices完整的办公软件,在安装目录下面有个MSWORD.OLB组件,导入这个即可。
1、相关操作Excel有MSPPT.OLB,EXCEL.exe等。
如何用python读取word
使用Python的内部方法open()读取文本文件try:f=open('/file','r')print(f.read())finally:if f:f.close()如果读取word文档推荐使用第三方插件,python-docx 可以在官网上下载使用方式# -*- coding: cp936 -*-import docxdocument = docx.Document(文件路径)docText = '\n\n'.join([paragraph.text.encode('utf-8') for paragraph in document.paragraphs])print docText
python open文件操作写入word为啥打不开
import codecsword="[ME]"utf16file=codecs.open('utf-16le.txt','r',encoding='utf-16-le')text=utf16file.read()utf16file.close()count=text.count(word)newfile=codecs.open('new.txt','w',encoding='utf-16-le')newfile.write(unichr(count).encode('utf-16-le'))for i in range(count):newfile.write(unichr(0).encode('utf-16-le'))newfile.write(text)newfile.close()offsetlist=[]newfile=codecs.open('new.txt','r',encoding='utf-16-le')text=newfile.read()newfile.close()length=len(word)offset=text.find(word)while(offset>=0):offsetlist.append(offset+length-1)offset=text.find(word,offset+length)newfile=codecs.open('new.txt','r+',encoding='utf-16-le')i=4Lfor offset in offsetlist:newfile.seek(i)newfile.write(unichr(offset).encode('utf-16-le'))i+=4newfile.close()
如何用python写word口令破解器
第一步。
方法1:开始——运行 winword /a 方法2:移除Normal.dot试一下。
Normal.dot通常位于: C:\Documents and Settings\UserName\Application Data\Microsoft\Templates\Normal.dot 注意:Application Data是个隐藏文件夹,请在文件夹选项中设置为显示所有文件。
方法3:删除HKEY_CURRENT_USER\Software\Microsoft\Office\ 下的所有键值,再再次启动word。
第二步。
“视图”——“工具栏”——勾选“常用”和“格式”
转载请注明出处51数据库 » python 操纵 word