PythonWin 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> import win32com.client
>>> word=win32com.client.gencache.EnsureDispatch('Word.Application')
>>> word.ActiveDocument.Tables[0].Cell(1,1).Range()
u'One\u4e00\r\x07'
请注意,它是一个Unicode字符串。Word似乎还使用\r\x07作为细胞系终止符。在
现在可以测试该值:
>>> value = word.ActiveDocument.Tables[0].Cell(1,1).Range()
>>> value == 'One' # NOTE: Python converts byte strings to Unicode via the default codec ('ascii' in Python 2.X)
False
>>> value == u'One'
False
>>> value == u'One马\r\x07'
False
>>> value == u'One一\r\x07'
True
>>> value == u'One\u4e00\r\x07'
True
>>> value == 'One\x92' # non-ASCII byte string fails to convert
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
False
转换为Unicode字符串,并在转换后使用
encode()
,然后对其进行转换:回到你原来的帖子:
ListTables[0].Cell(x,y)
从Word文档返回一个Cell
实例。对其调用str()
将检索其Unicode值,并尝试使用ascii
编解码器将其编码为字节字符串。因为它包含非ASCII字符,所以失败时会出现UnicodeEncodingError
。在在以后的编辑中:
^{pr2}$unicode
检索Unicode值,将其转换为UTF-8字节字符串,并将其存储在tyring
中。下一行尝试将字节字符串再次编码为“ascii”。这是无效的,因为只能对Unicode字符串进行编码,因此Python首先尝试使用默认的“ascii”编解码器将字节字符串转换回Unicode字符串。这将导致UnicodeDecodingError
(不是编码)。在最佳实践是用Unicode进行所有字符串处理。缺少的是
Range()
方法来获取单元格的值。下面是一个访问Word文档表的示例:请注意,它是一个Unicode字符串。Word似乎还使用
\r\x07
作为细胞系终止符。在现在可以测试该值:
试试这个,我想知道它是否有用:
if tr1_find.search(unicode(ListTables[0].Cell(x,y)).encode('utf-8','ignore')):
您还可以从Python的文档中找到这个页面,它很有帮助: http://docs.python.org/howto/unicode.html
它涵盖了这类问题。在
相关问题 更多 >
编程相关推荐