python-Excel: Chinese Characters

Z

zxo102

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,
'\xd6\xd7\xc1\xf6'))

Anybody knows what is going on with it?

Thanks for your help.

Ouyang
 
J

John Machin

zxo102 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top