Created
March 26, 2014 12:47
-
-
Save IsmiKin/9782340 to your computer and use it in GitHub Desktop.
Read excel in Django with xldr [intentona]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def testexcell(request): | |
book = open_workbook('pruebaexcel2.xlsx') | |
sheet = book.sheet_by_index(0) | |
#print "Name:",sheet.name | |
#print "Nrows",sheet.nrows | |
#print "Ncols",sheet.ncols | |
camposString = [ "zip", "latitude", "longitude" ] | |
camposDate = [ "modified_date", "publish_date" ] | |
columnas = [] | |
for row_index in range(sheet.nrows): | |
r = Restaurant() | |
for col_index in range(sheet.ncols): | |
# Cogemos la cabecera | |
if row_index == 0 : | |
columnas.append(sheet.cell(row_index,col_index).value) | |
else : | |
valor = sheet.cell(row_index,col_index).value | |
if type(valor) is FloatType : | |
valor = int(valor) | |
if(columnas[col_index] in camposDate): | |
tupla = xldate_as_tuple(valor,book.datemode) | |
fecha = '/'.join(map(str,tupla[0:3])) | |
hora = ":".join(map(str,tupla[4:6])) | |
formateado = " ".join([fecha,hora]) | |
print formateado | |
valor = formateado | |
if(columnas[col_index] in camposString): | |
valor = unicode(valor) | |
setattr(r, columnas[col_index], valor ) | |
print r.publish_date,r.modified_date | |
r.premium = False | |
r.revision = False | |
r.booking_reserv = False | |
#print r | |
r.save() | |
return render(request, 'viewrestaurant.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment