[英]Use python to call antiword to convert doc into txt, and find that the return value is garbled
問題描述
我用python調用antiword將doc轉換成txt,發現返回值的中文值是亂碼。 在powershell上也是一樣的情況。 但是調用 git bash 是可以的。
環境:Windows平台,py3.6
我嘗試更改語言環境並嘗試了antiword的-m參數,但它不起作用。
相關代碼
Pipe = subprocess.Popen(
['antiword', filename],
Stdout=subprocess.PIPE,
Stderr=subprocess.PIPE )
Stdout, stderr = pipe.communicate()
Return stdoutenter code here
不確定這是否會有所幫助,盡管我使用的是 Mac OS,但邏輯或多或少是相同的。
我認為您可以將['antiword',filename]
替換為 [ ['antiword','-m','utf-8',filename]
。
它所做的只是基本上調用antiword中提供的映射文件,將文件映射到utf-8.txt,然后通過stdout捕獲。 (對於其他的,你可以找到antiword提供的一些常用映射,例如8859和cp1251)
小提醒是檢查你的反字文件中的 .txt 的名稱。 我的一位朋友發現它是 UTF-8.txt,但在我的筆記本電腦上卻是 utf-8.txt。
BTW只是想抱怨antiword的編碼,也許我只是不熟悉字節操作因為我自學python。 antiword 的解碼很乏味。 特別是你有一個混合語言、頁眉和頁腳、格式奇怪的文檔,並使用了antiword的映射功能。 無論如何,祝你好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.