Python论坛  - 讨论区

标题:[python-chinese] 是用word com时出现的问题

2006年09月01日 星期五 19:53

nF n windhere在gmail.com
星期五 九月 1 19:53:16 HKT 2006

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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年09月01日 星期五 21:58

ainulinde ainulinde在gmail.com
星期五 九月 1 21:58:59 HKT 2006

好像结果是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
>

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年09月01日 星期五 22:15

nF n windhere在gmail.com
星期五 九月 1 22:15:35 HKT 2006

不对...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 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号