用Python将目录中的doc文件转换为docx文件

RS 技术•随笔评论848字数 2275阅读7分35秒阅读模式

要实现将目录中的doc文件转换为docx文件,可以使用Python中的`python-docx`库和`python-docx2pdf`库。以下是一个实现该功能的示例代码:

import os
from docx import Document
from docx2pdf import convert

# 获取当前目录路径
current_dir = os.getcwd()

# 遍历目录中的文件
for filename in os.listdir(current_dir):
    # 判断文件是否为doc文件
    if filename.endswith('.doc'):
        doc_file = os.path.join(current_dir, filename)
        # 使用python-docx库打开doc文件
        doc = Document(doc_file)

        # 设置新的文件名,并将".doc"替换为".docx"
        docx_filename = filename.replace('.doc', '.docx')
        docx_file = os.path.join(current_dir, docx_filename)

        # 保存为docx文件
        doc.save(docx_file)

        # 转换为pdf文件(可选)
        pdf_filename = filename.replace('.doc', '.pdf')
        pdf_file = os.path.join(current_dir, pdf_filename)
        convert(docx_file, pdf_file)

        print(f"转换成功:{docx_filename}")

上述代码中,首先需要安装`python-docx`和`python-docx2pdf`库。然后,使用`os`模块获取当前目录路径,并使用`os.listdir()`遍历目录中的文件。判断文件是否为.doc文件后,使用`python-docx`库打开doc文件,并使用`.save()`方法将其保存为docx文件。可以使用`python-docx2pdf`库将docx文件转换为pdf文件(可选)。最后,在控制台打印转换成功的文件名。

请注意,此代码假设你的目录中只有需要转换的doc文件,并且没有子目录。如果你的目录结构复杂或包含其他类型的文件,请相应地修改代码。


程序运行时,显示is not a Word file,content type is 'application/vnd.openxmlformats-officedocument.themeManager+xml'这是什么意思,怎么解决?

这个错误提示表明程序在打开文档时遇到了一个不支持的文件类型。根据错误消息中提到的内容类型 `'application/vnd.openxmlformats-officedocument.themeManager+xml'`,可以看出尝试打开的文件实际上是一个主题管理器文件(Theme Manager file),而不是一个有效的Word文档文件。

这可能是因为在目录中存在了不正确的文件类型或扩展名不匹配的文件。要解决这个问题,可以在代码中添加一些文件类型的验证和过滤。

以下是更新后的代码示例,添加了文件类型的检查和过滤功能:

import os
import magic  # 借助第三方库进行文件类型检查
from docx import Document
from docx2pdf import convert

# 获取当前目录路径
current_dir = os.getcwd()

# 遍历目录中的文件
for filename in os.listdir(current_dir):
    # 获取文件路径
    file_path = os.path.join(current_dir, filename)
    
    # 使用第三方库进行文件类型检查
    file_type = magic.from_file(file_path, mime=True)
    
    # 判断文件类型是否为doc
    if file_type == 'application/msword':
        # 使用python-docx库打开doc文件
        doc = Document(file_path)

        # 设置新的文件名,并将".doc"替换为".docx"
        docx_filename = filename.replace('.doc', '.docx')
        docx_file = os.path.join(current_dir, docx_filename)

        # 保存为docx文件
        doc.save(docx_file)

        # 转换为pdf文件(可选)
        pdf_filename = filename.replace('.doc', '.pdf')
        pdf_file = os.path.join(current_dir, pdf_filename)
        convert(docx_file, pdf_file)

        print(f"转换成功:{docx_filename}")
    else:
        print(f"忽略文件:{filename}")

在更新后的代码中,引入了`magic`库来进行文件类型检查。使用`magic.from_file(file_path, mime=True)`可以获取文件的MIME类型。在判断文件类型为`'application/msword'`时,才会将其视为doc文件,并进行后续的处理。对于其他类型的文件,程序将会显示忽略该文件的消息。

请确保已安装依赖的`magic`库,可以使用`pip install python-magic`命令进行安装。如果还是遇到问题,可能需要检查文件的实际类型和扩展名是否匹配,或者手动排除不正确的文件。

继续阅读
 
RS
  • 本文由 RS 发表于 2023年6月21日 10:31:02
助你整理杂乱无章的文件 技术•随笔

助你整理杂乱无章的文件

今天的小程序,可以一键完成文件整理,一起来看看吧! 按照文件格式整理 具体来说就是不同的文件后缀名称放置到不同的文件夹当中 今天我们只需要使用 os 和 shutil 两个库就足够了。os 是 Pyt...
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定