出现以下错误
print Text
File "C:\Python27\lib\encodings\cp437.py", line 12, in encodereturn codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2026' in position 0:
character maps to undefined
我正在使用的代码是
import win32com.client
import os
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = True
Document = MSWord.Documents.Open(os.getcwd()+'\\' + 'MARS.doc')
for paragraph in Document.Paragraphs:
Text = paragraph.Range.Text
print Text
答案 0 :(得分:0)
您的文字具有无法打印到标准输出的unicode字符。尝试
Document = MSWord.Documents.Open(os.getcwd()+'\\' + 'MARS.doc')
counter = 0
for paragraph in Document.Paragraphs:
counter += 1
Text = paragraph.Range.Text
print "paragraph to edit:", counter, ":"
print str(Text).encode('ascii', 'replace')
这样,不可打印的字符将显示为“?”。但是如果你在你的问题中澄清你真正想要做什么(可能是一些文本处理),你会得到更多有用的答案。