Python脚本:批量将.doc文件转化为.docx文件
将.doc转换为.docx文件有几种常用的方法:
- Microsoft Word 和 WPS 自带.doc转换.docx功能,但只能一个文件一个文件转换,批量转换要会员
- 在线网页 Office-Converter.com 等在线网页转换工具,以前很好用,现在也开始收费了
作为白嫖党,不想花钱冲会员又不想一个个打开一个个转换,故写此脚本。
1 import os
2 import time
3 from win32com import client as wc
4
5 path1 = 'E:/waiting/' # 需要修改的文件的路径
6 path2 = 'E:/saving/' # 存储的路径
7
8 for file in os.listdir(path1):
9
10 if file.endswith('.doc'):
11 word = wc.Dispatch("Word.Application")
12 out_name = file.replace("doc", r'docx') # doc文件修改后缀名
13 in_file = os.path.abspath( path1 + "\\" + file)
14 out_file = os.path.abspath( path2 + "\\" + out_name)
15 print(in_file)
16 print(out_file)
17
18 doc = word.Documents.Open(in_file)
19 doc.SaveAs(out_file, 12, False, "", True, "", False, False, False, False)
20 doc.Close()
21
22 print('转换成功')
23 word.Quit()
24 time.sleep(3) # 避免文件未关闭就打开下一个文件
25
26 else:
27 word = wc.Dispatch("Word.Application")
28 out_name = file # 不是doc文件则不修改后缀名
29 in_file = os.path.abspath( path1 + "\\" + file)
30 out_file = os.path.abspath( path2 + "\\" + out_name)
31 doc = word.Documents.Open(in_file)
32 print(in_file)
33 print(out_file)
34
35 doc.SaveAs(out_file, 12, False, "", True, "", False, False, False, False)
36 doc.Close()
37
38 print('复制成功')
39 word.Quit()
40 time.sleep(3) # 避免文件未关闭就打开下一个文件
41
直接复制代码,修改 path1 和 path2 路径即可使用,取走请留言。

浙公网安备 33010602011771号