I'm trying to create a kml from a xlsx file, but when xlsx contains utf-8 it's not working.
I saw simplekml documentation and it's marked as solved, but I can't make it work.
I've tried setting the encode to ascii, also used django's smart_str and smart_unicode, but nothing worked until now.
I'm reading the file using openpyxl
def create_kml(input_file,sheet_name,output_file,lat_column = 0,lng_column = 1):
kml = simplekml.Kml()
wb_read = load_workbook(input_file)
sh = wb_read.get_sheet_by_name(sheet_name)
properties = []
for c in sh.rows[0]:
properties.append(c.value.encode('utf-8'))
for p,row in enumerate(sh.rows[1:]):
for k,c in enumerate(row):
if k==0:
coord_tuple = (row[lng_column].value,row[lat_column].value)
pnt = kml.newpoint(name = 'Point %s' % p, coords =[coord_tuple])
#TODO: It's not working with unicode and utf-8
if k != lat_column and k != lng_column:
if type(row[k].value) == unicode or type(row[k].value) == str:
pnt.extendeddata.newdata(properties[k],row[k].value.encode('utf-8'))
kml.save(output_file)
Traceback:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:/Users/Fernando Alves/Dropbox/Projetos/utils.py", line 296, in create_kml
kml.save(output_file)
File "C:\Python27\lib\site-packages\simplekml\kml.py", line 285, in save
out = self._genkml(format)
File "C:\Python27\lib\site-packages\simplekml\kml.py", line 198, in _genkml
kml_str = self._feature.__str__()
File "C:\Python27\lib\site-packages\simplekml\featgeom.py", line 418, in __str__
buf.append(feat.__str__())
File "C:\Python27\lib\site-packages\simplekml\featgeom.py", line 414, in __str__
buf.append(super(Feature, self).__str__())
File "C:\Python27\lib\site-packages\simplekml\base.py", line 54, in __str__
buf.append(u("<{0}>{1}</{0}>").format(var, val)) # Enclose the variable's __str__ with its name
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 33: ordinal not in range(128)