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()
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()
相关文章推荐
- python中的多态
- Python学习之安装WingIDE并破解
- PEP8 Python 编码规范整理
- python函数函数指针用法
- from __future__ import absolute_import
- python -- 抽奖
- Requests+Beautifulsoup+re百度贴吧抓取实例(python3.4逐行解释)
- 怎么解决python "Non-ASCII character"错误
- (1)python基本使用
- Python2.*脚本转换成Python3.*脚本
- leetcode之Search in Rotated Sorted Array
- 【Python】调用WPS V9 API,实现PPT转PDF
- leetcode之Search a 2D Matrix II
- Learn Python The Hard Way(0--26)
- leetcode之Search a 2D Matrix
- python例子-开始一个Django项目
- 零基础学python-19.3 列表解析与矩阵
- 零基础学python-18.8 嵌套lambda与作用域
- 零基础学python-18.1 函数的设计
- 零基础学python-18.7 为什么要使用lambda表达式