推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
CppOrJava
V2EX  ›  Python

用 antiword 将 doc 转换成 txt,怎么解决返回结果中文乱码问题?

  •  2
     
  •   CppOrJava · Nov 3, 2018 · 4082 views
    This topic created in 2882 days ago, the information mentioned may be changed or developed.

    问题描述

    使用 python 调用 antiword 将 doc 转换成 txt,发现返回值是乱码,powershell 上用 antiword 也是乱码,在 git bash 上调用就没问题

    环境背景及自己尝试过哪些方法

    环境:windows 平台,py3.6 尝试过改 locale,还试过 antiword 的-m 参数,但是没什么用

    相关代码

    pipe = subprocess.Popen(
            ['antiword', filename],
            stdout=subprocess.PIPE, 
            stderr=subprocess.PIPE  )
    
    stdout, stderr = pipe.communicate()
    return stdout
    
    5 replies  •  2018-11-04 10:09:05 +08:00
    newghost
        1
    newghost  
       Nov 3, 2018
    pdf2text
    lihongjie0209
        2
    lihongjie0209  
       Nov 3, 2018
    每一个进程执行的都有上下文的, 比如说环境变量, 你在 Python 中把环境变量打出来看看编码的配置项
    CppOrJava
        3
    CppOrJava  
    OP
       Nov 3, 2018
    @lihongjie0209 我试过改 locale,没有用
    代码:

    locale.setlocale(locale.LC_ALL, 'zh_CN.UTF-8')
    locale.setlocale(locale.LC_COLLATE,'zh_CN.UTF-8')
    locale.setlocale(locale.LC_CTYPE,'zh_CN.UTF-8')
    locale.setlocale(locale.LC_NUMERIC,'zh_CN.UTF-8')
    locale.setlocale(locale.LC_MONETARY,'zh_CN.UTF-8')
    locale.setlocale(locale.LC_TIME,'zh_CN.UTF-8')
    CppOrJava
        4
    CppOrJava  
    OP
       Nov 3, 2018
    @lihongjie0209 Linux 和 Windows 下 git bash 是正常的
    其实感觉跟 antiword 关系更大一些, cmd 下 chcp 65001 后 antiword 的输出仍然会有乱码
    但在 git bash 下调用 antiword, 就不会输出乱码
    zjsxwc
        5
    zjsxwc  
       Nov 4, 2018 via Android
    GB2312 的问题吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2637 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 03:34 · PVG 11:34 · LAX 20:34 · JFK 23:34
    ♥ Do have faith in what you're doing.