python-Excel: Chinese Characters

John Machin sjmachin at lexicon.net
Sun Aug 14 23:06:30 EDT 2005


zxo102 wrote:
> Hi there,
>     I am trying to put data including Chinese Characters into Excel
> through python. But I got some problems. Here is my sample code:
> 
> ##################################################
> #
> import win32com.client
> xlapp  = win32com.client.DispatchEx("Excel.Application")
> xlbook = xlapp.Workbooks.Open("test.xls")
> sht    = xlbook.Worksheets("Sheet1")
> 
> # data is provided in case 1 and case 2 below
> ...
> sht.Range(sht.Cells(1, 1), sht.Cells(2,4)).Value = data
> #
> ##################################################
> 
> Using the above code, I tested two cases for "data". The "data" from
> both two cases are same: tuple basically. But in case 1, only 2222 and
> 3333 are inserted into Excel. All other cells are blank. in case2, all
> data including Chinese Characters are inserted into Excel and
> everything is fine. My real data is like case 1.
> 
> 
> 
> #case 1 ------------------------------------------
> rp = {'r1':['\xd6\xd7\xc1\xf6','\xd6\xd7\xc1\xf6'],
>       'r2':['\xd6\xd7\xc1\xf6','\xd6\xd7\xc1\xf6'],
>       'r3':[2222,3333],
>       'r4':['\xd6\xd7\xc1\xf6','\xd6\xd7\xc1\xf6']
>      }
> 
> b = ()
> for k in range(len(rp['r1'])):
>     a = rp['r1'][k],rp['r2'][k],rp['r3'][k],rp['r4'][k]
>     if len(b) == 0:
>         b = a
>     else:
> 	b = b,a
> data = b
> 
> #case 2 ------------------------------------------
> data   =(('\xd6\xd7\xc1\xf6', '\xd6\xd7\xc1\xf6', 2222,
> '\xd6\xd7\xc1\xf6'),
>         ('\xd6\xd7\xc1\xf6", '\xd6\xd7\xc1\xf6", 3333,

Bulldust alert! Syntax error caused by starting 2 strings with ' and 
ending with "

> '\xd6\xd7\xc1\xf6'))
> 
> Anybody knows what is going on with it?

No. How could we know??? Try showing us *EXACTLY* what code you ran.

FWIW, correcting the " to ' above shows no difference in the two cases:

C:\junk>type ouyang.py
rp = {'r1':['\xd6\xd7\xc1\xf6','\xd6\xd7\xc1\xf6'],
       'r2':['\xd6\xd7\xc1\xf6','\xd6\xd7\xc1\xf6'],
       'r3':[2222,3333],
       'r4':['\xd6\xd7\xc1\xf6','\xd6\xd7\xc1\xf6']
      }

b = ()
for k in range(len(rp['r1'])):
     a = rp['r1'][k],rp['r2'][k],rp['r3'][k],rp['r4'][k]
     if len(b) == 0:
         b = a
     else:
         b = b,a
data1 = b

#case 2 ------------------------------------------
data2 =(('\xd6\xd7\xc1\xf6', '\xd6\xd7\xc1\xf6', 2222, '\xd6\xd7\xc1\xf6'),
         ('\xd6\xd7\xc1\xf6', '\xd6\xd7\xc1\xf6', 3333,'\xd6\xd7\xc1\xf6'))

print
print "data1", repr(data1)
print "data2", repr(data2)
print data1 == data2

C:\junk>ouyang.py

data1 (('\xd6\xd7\xc1\xf6', '\xd6\xd7\xc1\xf6', 2222, 
'\xd6\xd7\xc1\xf6'), ('\xd
6\xd7\xc1\xf6', '\xd6\xd7\xc1\xf6', 3333, '\xd6\xd7\xc1\xf6'))
data2 (('\xd6\xd7\xc1\xf6', '\xd6\xd7\xc1\xf6', 2222, 
'\xd6\xd7\xc1\xf6'), ('\xd
6\xd7\xc1\xf6', '\xd6\xd7\xc1\xf6', 3333, '\xd6\xd7\xc1\xf6'))
True



More information about the Python-list mailing list