您的位置:首页 > 编程语言 > Python开发

python脚本——合并word

2015-10-21 10:36 465 查看
#-*- coding: utf-8-*-

import os

import sys

import fnmatch

import glob

from win32com import client as wc

#将文件夹下的word文件转化为txt文件

def word2txt():

path='D:\code\my_projects\project01\source'

word=wc.gencache.EnsureDispatch('Word.Application')

try:

for path,dirs,files in os.walk(path):

for filename in files:

if not fnmatch.fnmatch(filename,'*.doc'):continue

doc=os.path.abspath(os.path.join(path,filename))

print 'processing %s...' % doc

word.Documents.Open(doc)

docastext=doc[:-3]+'txt'

word.ActiveDocument.SaveAs(docastext,FileFormat=wc.constants.wdFormatText)

word.ActiveDocument.Close()

finally:

word.Quit()

#将转化得到的txt文件合并成一个txt

def txt_merge():

finlist=glob.glob('D:\code\my_projects\project01\source\*.txt')

f=open('D:\code\my_projects\project01\source\p01.txt','w')

for fin in finlist:

x=open(fin,'r')

f.write(x.read())

x.close()

f.close()

#将合并的txt文件修改为word文件(这里直接修改后缀名)、删除得到的txt文件

def txt2word():

portion=os.path.splitext('D:\code\my_projects\project01\source\p01.txt')

newname=portion[0]+'.doc'

os.rename('D:\code\my_projects\project01\source\p01.txt',newname)

txtlist=glob.glob('D:\code\my_projects\project01\source\*.txt')

for txtfile in txtlist:

os.remove(txtfile)

print u'合并后的文件为p01.doc'

#主函数

def main():

word2txt()

txt_merge()

txt2word()

if __name__ == "__main__":

main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: