Python用win32com调用word SaveAs2为文本文件时,内容有多语言,如泰语、俄语等编码设置为UTF-8 ,65001,另存为文本文件.txt内容为乱码
按照MSDN Document.SaveAs2 方法设置参数,但没有变化,设置了参数
FileFormat,Encoding 但是不起作用,望有经验的指导一二。
主要处理代码
import os
import chardet
import sys
import re
import os.path
from win32com import client as wc
......
word = wc.Dispatch('Word.Application')
def merge(file_name,file_path,old_file_name):
if (not (os.path.basename(file_name)).startswith('~') and file_name.split('.')[-1] == 'docx'):
doc = word.Documents.Open(file_name)
newname= file_name[:-5]+".txt"
try:
Encoding1 = 65001
doc.SaveAs2(FileName = newname,FileFormat = 2 ,Encoding = Encoding1)
print(file_name + ' 转换文件成功!')
except Exception as e:
print('**********' + file_name + ' 转换失败!**********')
pass
doc.Close()
这里的参数设置了Encoding没有用
doc.SaveAs2(FileName = newname,FileFormat = 2 ,Encoding = Encoding1)