栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python - 操作 doc / docx

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python - 操作 doc / docx

文章目录
    • 读取 docx
    • 读取 doc


注意:doc/docx 文件名中如果包含空格,可能导致读取失败,可以提前替换为 _


读取 docx

textract 和 docx2txt 两个库都支持读取 docx

import textract
import docx2txt

file_path = '.../docx/a.docx'
print('file_path : ', file_path)
bs = textract.process(file_path)  # 得到的是 bytes
text1 = bs.decode('utf-8')

text2 = docx2txt.process(file_path)


读取 doc

安装 antiword
参考:https://blog.csdn.net/Webben/article/details/103343828


def test2():

    root_dir = '../doc/'
    txt_dir = root_dir + 'txt/'

    command_path = '/home/xx/software/antiword-0.37/antiword'

    for file_name in os.listdir(root_dir):

        file_id = file_name.replace('.doc', '')
        print('n-- ', file_id)

        doc_path = root_dir + file_name
        txt_path = txt_dir + file_id + '.txt'
 
        script = f'{command_path} -f {doc_path} > {txt_path} '
        try:
            ret = os.system(script) 
        except Exception as err:
            print('xx error : ', file_name)
            continue



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/461476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号