python实现将一个目录下的所有doc文件转为docx格式文件

from win32com import client as wc
from docx import Document
import shutil
def file_clear(dir_path):
	"""文件清理,将doc文件转为docx文件"""
	if not os.path.isdir(dir_path + 'docx):
		os.mkdir(dir_path + 'docx')
	all_file = os.listdir(dir_path)
	word = wc.Dispath('word.Application')
	for path in all_file:
		if path.endswith('.doc') or path.endswith('.DOC'):
			doc = word.Documents.Open(dir_path + '\\' + path)
			if path.endswith('.DOC'):
			doc.SaveAs(dir_path + 'docx' + '\\' + path[0:-4] + 'docx', 12, False, "", True, "", False, False, False, False)
			else:
				doc.SaveAs(dir_path + 'docx' + '\\' + path + 'x', 12, False, "", True, "", False, False, False, False)
		else:
			shutil.copy(dir_path + '\\' + path, dir_path + docx + '\\' path)
	word.Quit()			

此函数可以复制过去调用,python2的需要把字符编码后使用

你可能感兴趣的:(Python)