1.python读取word文档内容
import fnmatch, os, sys, win32com.client
readpath=r'D:\123'
wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")
try:
for path, dirs, files in os.walk(readpath):
for filename in files:
if not fnmatch.fnmatch(filename, '*.docx'):continue
doc = os.path.abspath(os.path.join(path,filename))
print 'processing %s。' % doc
wordapp.Documents.Open(doc)
docastext = doc[:-4] + 'txt'
wordapp.ActiveDocument.SaveAs(docastext,FileFormat=win32com.client.constants.wdFormatText)
wordapp.ActiveDocument.Close()
finally:
wordapp.Quit()
print 'end'
f=open(r'd:\123\test.txt','r')
for line in f.readlines():
print line.decode('gbk')
f.close()
2.python操作word文档,用win32com,如何用python中的变量来命名
import sys, time, string, win32com.client, stat, osclass CWordAutomate: """封装word com 连接""" def __init__( self ): """创建到word的ole连接对象""" self.m_obWord = win32com.client.Dispatch( "Word.Application" ) self.m_obDoc = self.m_obWord.Documents.Add( ) #创建文档 self.m_obWord.Visible = 1 self.m_Sel = self.m_obWord.Selection # 获取选择对象 def WriteLine( self, sTxt, sFont, lSize, bBold=0 ): """写一行到文档""" self.m_Sel.Font.Name = sFont self.m_Sel.Font.Bold = bBold self.m_Sel.Font.Size = lSize self.m_Sel.TypeText( Text=sTxt + "\n" ) def Save(self, sFilename): self.m_obDoc.SaveAs(sFilename) def Quit(self): self.m_obWord.Quit()def file_test(file): """ 测试用户提供的文件,查看它是否存在并包含数据。
如果输入文件不存在或是空的,则返回一个警告代码 """ if (0 == os.path.isfile(file) or (0 == os.stat(file)[stat.ST_SIZE])): return 1 else: return 0if __name__ == "__main__": usage = "\n\n\tUsage: msword.py {inputfile} {outputfile}\n" # # 测试传入数字的参数 # if len(sys.argv) != 3: print "\n\n\tmsword.py error: \n\n\tInsufficient arguments passed." print usage sys.exit(1) # 测试源文件是否存在并包含数据 if file_test(sys.argv[1]) == 1 : print "\n\n\tmsword.py error: \n\n\tSource file not found or is empty." print usage sys.exit(1) # 测试目标文件,避免意外导致崩溃 if file_test(sys.argv[2]) == 0 : print "\n\n\tmsword.py error: \n\n\tTarget file already exists." print usage sys.exit(1) sFileName = sys.argv[1] obFile = file( sFileName, 'r+' ) sContent = obFile.read() obFile.close() lstContent = sContent.splitlines() # # 写入数据 # obWord = CWordAutomate() for sLine in lstContent: obWord.WriteLine( sLine, "Courier New", 10 ) sLastMsg = time.strftime( "document generated on %c", time.localtime() ) obWord.WriteLine( sLastMsg, "Times New Roman", 14, 0 ) obWord.Save(sys.argv[2]) obWord.Quit()。
3.怎样安装win32com.client
从下面的参考地址里,找到pywin32各个版本下载地址。
打开的页面里,你会发现有几十个版本的pywin32,怎么找呢?比如我的电脑是win7 64位,python版本是2.7,我找到的是下面这个。点击它就能直接下载。
下载完成以后,这是一个exe文件,直接双击安装就可以了。点击下一步。
在第二步,你会看到你的python安装目录,如果没有检测到你的python安装目录,八成你现在的pywin32版本是不对的,重新下载。点击下一步
4.电脑上的word为什么总是自动关闭
word文件自动关闭有以下几种原因: 1、word系统有问题; 2、word与输入法有冲突; 3、电脑有病毒; 4、word默认模版有问题; 5、与打印机驱动冲突,或者打印机驱动程序故障。
解决方法: 根据由易到难的顺序逐一排除: 1、先给电脑杀毒,杀毒完成后依然无法解决,进行第二步; 2、更改默认的输入法,或先删除默认的输入法,若依然无法解决,进行第三步; 3、删除C:\Documents and Settings\Administrator\Application Data\Microsoft\Templates文件夹下面的文件,若依然无法排除,进行第四步; 4、重新安装office程序,或者升级office程序,或者更换为WPS(WPS兼容所有office版本),若依然无法解决,进行第五步; 5、删除打印机驱动程序,重新安装驱动程序,问题解决。 一般不用5步就能解决问题。
若以上五步均不能解决问题,那就是电脑系统有问题,只有重新安装系统程序了。
5.python 调win32com 操作excel AutoFilter怎么用
#coding=gbk
import win32com.client,os
excel=win32com.client.Dispatch('Excel.Application')
excel.Workbooks.Open(os.path.abspath('sy.xls'))
excel.Selection.AutoFilter(Field=2, Criteria1="移动".decode('gbk'))
excel.Quit()
转载请注明出处51数据库 » win32comwordclose