2006年09月01日 星期五 19:53
w = win32com.client.Dispatch('Word.Application')
doc = w.Documents.Open('test.txt')
print doc.Paragraphs(1)
若第一段里有中文,
则会出现以下错误
Traceback (most recent call last):
File "H:/Py/doctest.py", line 31, in -toplevel-
print doc.Paragraphs(1)
File "D:\Python24\Lib\site-packages\win32com\client\dynamic.py", line 187,
in __str__
return str(self.__call__())
File "D:\Python24\Lib\site-packages\win32com\client\dynamic.py", line 187,
in __str__
return str(self.__call__())
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7:
ordinal not in range(128)
请问如何解决此问题?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20060901/460cfb95/attachment.html
2006年09月01日 星期五 21:58
好像结果是unicode的,试试
print doc.Paragraphs(1).encode('cp936')
On 9/1/06, nF n <windhere at gmail.com> wrote:
>
> w = win32com.client.Dispatch('Word.Application')
> doc = w.Documents.Open('test.txt')
> print doc.Paragraphs(1)
>
> 若第一段里有中文,
> 则会出现以下错误
> Traceback (most recent call last):
> File "H:/Py/doctest.py", line 31, in -toplevel-
> print doc.Paragraphs(1)
> File
> "D:\Python24\Lib\site-packages\win32com\client\dynamic.py",
> line 187, in __str__
> return str(self.__call__())
> File
> "D:\Python24\Lib\site-packages\win32com\client\dynamic.py",
> line 187, in __str__
> return str(self.__call__())
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7:
> ordinal not in range(128)
>
> 请问如何解决此问题?
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
2006年09月01日 星期五 22:15
不对...Paragraphs(index) 返回的是一个paragraph对象,它并没有encode方法, 只是有到string 的转换,就是转换的过程中有的问题... 暂时的理解...-.- 怎么解决阿..555 On 9/1/06, ainulinde <ainulinde at gmail.com> wrote: > > 好像结果是unicode的,试试 > print doc.Paragraphs(1).encode('cp936') > > On 9/1/06, nF n <windhere at gmail.com> wrote: > > > > w = win32com.client.Dispatch('Word.Application') > > doc = w.Documents.Open('test.txt') > > print doc.Paragraphs(1) > > > > 若第一段里有中文, > > 则会出现以下错误 > > Traceback (most recent call last): > > File "H:/Py/doctest.py", line 31, in -toplevel- > > print doc.Paragraphs(1) > > File > > "D:\Python24\Lib\site-packages\win32com\client\dynamic.py", > > line 187, in __str__ > > return str(self.__call__()) > > File > > "D:\Python24\Lib\site-packages\win32com\client\dynamic.py", > > line 187, in __str__ > > return str(self.__call__()) > > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 0-7: > > ordinal not in range(128) > > > > 请问如何解决此问题? > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to > > python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: > > http://python.cn/mailman/listinfo/python-chinese > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20060901/9cf4ed72/attachment.html
Zeuux © 2025
京ICP备05028076号