惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
I
InfoQ
F
Full Disclosure
Vercel News
Vercel News
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
GbyAI
GbyAI
MyScale Blog
MyScale Blog
L
LangChain Blog
云风的 BLOG
云风的 BLOG
T
The Exploit Database - CXSecurity.com
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
Engineering at Meta
Engineering at Meta
Scott Helme
Scott Helme
Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Simon Willison's Weblog
Simon Willison's Weblog
The Hacker News
The Hacker News
阮一峰的网络日志
阮一峰的网络日志
T
Threatpost
D
DataBreaches.Net
Jina AI
Jina AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
V
Vulnerabilities – Threatpost
T
Tailwind CSS Blog
N
News | PayPal Newsroom
AI
AI
N
News and Events Feed by Topic
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
I
Intezer
Schneier on Security
Schneier on Security
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
美团技术团队
B
Blog RSS Feed
小众软件
小众软件
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events

博客园 - pcwanli

Linux下版本控制器(SVN) -命令行客户端 linux svn 命令 Qwen3-VL视频科技:内容审核系统搭建 svn update 出现Skipped 'feifazuzhi' -- Node remains in conflict处理方法 linux redis.service如何编写 深入php redis pconnect PHP中使用Redis长连接笔记 python happybase 批量读取 如何用phpredis持久化连接pconnect方法提升应用响应速度 python处理常见格式压缩包文件的全指南 Python编程:happybase读写HBase数据库 python redis zset 按分值获取记录 hbase日志如何清理 hbase日志清理 Python的time.strftime()方法 python re.sub第二参数,前值如何引用 【Python】基于python实现Windows Service程序 python解析url参数 提取网页源码头信息的正则表达式 PHP字符串分割:explode()函数详解与应用 python requests get请求禁用自动解压 python importlib动态模块加载遇到is not a package错误,解决方法。 Python正则表达式替换(re.sub)的6种典型应用场景 用Python处理HTML转义字符的5种方式 pymongo批量更新bulk_write php mongodb操作 PHP 通过 Thrift 操作 Hbase 使用thrift的php版本操作hbase数据库 PHP通过Thrift操作Hbase
Python批量将Word文档(.doc)转换为.docx格式的完整实现步骤
pcwanli · 2026-06-18 · via 博客园 - pcwanli

Posted on 2026-06-18 10:09  pcwanli  阅读(0)  评论()    收藏  举报

来源:https://www.jb51.net/python/33264325j.htm

前言

在日常办公中,我们经常会遇到 .doc 和 .docx 格式的 Word 文件。尽管 .doc 是旧版 Word 使用的格式,但为了兼容性和功能的完整性,现代办公需求更倾向于使用 .docx 格式。这篇文章将介绍如何使用 Python 自动批量将 .doc 格式的文件转换为 .docx 格式,方便我们快速转换大量文件。

一、环境准备

1. 安装Python和必要的库

首先,我们需要 Python 环境,并且安装 comtypes 库,用于与 Windows 的 COM 组件(如 Word)进行交互:

2. 确保安装了Microsoft Word

因为 comtypes 调用了 Word 的 COM 接口,因此需要确保系统中已安装了 Microsoft Word(适用于 Windows 系统)。

二、代码实现

1. 导入所需库

我们首先导入 os 和 comtypes.client,分别用于文件路径操作和与 Word 交互:

1

2

import os

import comtypes.client

2. 指定文件夹路径

在代码中,我们指定了包含 .doc 文件的文件夹路径。在示例中,将路径设置为 D:\1,请根据需要替换为你实际存放文件的路径:

3. 编写转换函数

函数 convert_doc_to_docx(input_doc_path) 将单个 .doc 文件转换为 .docx 文件:

  • 使用 comtypes.client.CreateObject('Word.Application') 创建 Word 应用实例。
  • 打开 .doc 文件,并指定保存为 .docx 格式。
  • 关闭文档和 Word 应用,释放资源。

1

2

3

4

5

6

7

8

9

def convert_doc_to_docx(input_doc_path):

    word = comtypes.client.CreateObject('Word.Application')

    word.Visible = False

    doc = word.Documents.Open(input_doc_path)

    output_docx_path = input_doc_path.replace('.doc', '.docx'

    doc.SaveAs(output_docx_path, FileFormat=16

    doc.Close()

    word.Quit()

    return output_docx_path 

4. 批量转换文件

接下来,我们遍历文件夹中的 .doc 文件,调用 convert_doc_to_docx 函数逐个转换。每次转换完成后,将输出转换成功的文件名和路径。

1

2

3

4

5

6

7

8

for filename in os.listdir(folder_path):

    if filename.endswith('.doc'):

        doc_path = os.path.join(folder_path, filename)

        docx_path = convert_doc_to_docx(doc_path) 

        print(f"{filename} 已成功转换为 {docx_path}")

print("所有文件已转换完成!")

三、完整代码

以下是将 .doc 文件批量转换为 .docx 的完整代码,包含注释方便理解:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import os

import comtypes.client

folder_path = r'D:\1'

def convert_doc_to_docx(input_doc_path):

    word = comtypes.client.CreateObject('Word.Application')

    word.Visible = False 

    doc = word.Documents.Open(input_doc_path) 

    output_docx_path = input_doc_path.replace('.doc', '.docx'

    doc.SaveAs(output_docx_path, FileFormat=16

    doc.Close() 

    word.Quit() 

    return output_docx_path 

for filename in os.listdir(folder_path):

    if filename.endswith('.doc'):

        doc_path = os.path.join(folder_path, filename)

        docx_path = convert_doc_to_docx(doc_path) 

        print(f"{filename} 已成功转换为 {docx_path}")

print("所有文件已转换完成!")

四、代码说明

  • 文件遍历与判断

    • os.listdir(folder_path) 遍历文件夹中的所有文件。
    • if filename.endswith('.doc') 确保只处理 .doc 文件,避免误处理其他文件格式。
  • Word应用不可见

    • word.Visible = False 隐藏 Word 窗口,避免弹出影响用户操作。
  • 文件路径替换

    • output_docx_path = input_doc_path.replace('.doc', '.docx') 将原文件路径的扩展名从 .doc 改为 .docx,生成新的保存路径。
  • 文件格式设置

    • FileFormat=16 指定保存为 .docx 格式。FileFormat 的参数值 16 对应 .docx 文件类型。
  • 资源释放

    • doc.Close() 关闭当前文档,word.Quit() 退出 Word 应用,确保不占用资源。
  • 输出结果

    • 每次成功转换后,使用 print() 显示已完成转换的文件名,便于跟踪进度。

五、注意事项

  • 确保系统中安装了 Microsoft Word:代码依赖于 Word 的 COM 组件,系统中需要安装 Word 才能正常运行。
  • 文件格式和路径:请确认 .doc 文件的路径,避免指定错误的文件夹。
  • 资源管理:转换大量文件时,确保 word.Quit() 被执行,避免 Word 进程占用系统资源。
  • 运行环境:此代码适用于 Windows 系统,因其依赖 COM 组件来与 Word 进行交互。 

六、总结

通过使用 Python 与 comtypes 库,我们能够实现批量将 .doc 文件转换为 .docx 文件的需求。