Take a look at the builtin enumerate() function.

Cheers,
AT

On Thu, Aug 11, 2011 at 11:33 AM, Dipo Elegbede
<delegb...@timbaobjects.com>wrote:

> Hello All,
>
> I have a little problem, I,d like to share with you for possible solution.
>
> I have a models like this:
>
> class MyBuddyt(models.Model):
>
>     location = models.CharField(blank=True, max_length=200)
>     name = models.CharField(blank=True, max_length=200)
>     email = models.CharField(blank=True, max_length=200)
>     comment = models.CharField(blank=True, max_length=200)
>     phone = models.DateCharField(blank=True, max_length=200)
>
> I have created over 90 buddies and these data lies in my database.
>
> I want to export these data from the database straight to an Excel file i.e
> *.xls.
>
> I know this is doable using python csv module but that's not what I want.
>
>  I have tried using the xlrd and xlwt package and I am sure they can get
> it done, I just haven't been able to get my way around.
>
> If I do this:
> buddies = MyBuddy.objects.all()
> for buddy in buddies:
>  print buddy.name, buddy.location, buddy.email, buddy.phone, buddy.comment
>
> I get 90 line of information as ordered by the print function, with each
> line having 5 values.
>
> Now I want this to go into an Excel file so that each line represents a row
> and each of the 5 values per role stays in different columns on that row e.g
> buddy.name in cell(1,a) , buddy.location in cell(2,a),
> buddy.email in cell(3,a), buddy.phone in cell(4,a),
> buddy.comment in cell(5,a)
>
> How do I get this done to happen for the 90 entries.
>
> I have tried this:
>
> for buddy in buddies:
>   for row in range(r+1):
>    for col in range(4):
>     sh.write(row,col,'%s' % (buddy))
>
> I got the error:
>
> Exception: Attempt to overwrite cell: sheetname=u'names' rowx=0 colx=0
>
>  Did this:
>
> for buddy in buddies:
>     colm = 0
>     row = 1
>     for item in buddyt:
>      sh.write(row, colm, item)
>      bk.save('C:\Users\owner\Desktop\DEL\tester.xls')
>
> Got this error:
>
> TypeError                                MyBuddy object not iterable
>
> and also this:
>
> for contact in contacts:
>  row=0
>  col=0
>  r=90
>  while row < r:
>   sh.write(row,col,buddy.name)
>   col += 1
>   sh.write(row,col,buddy.location)
>    col += 1
>   sh.write(row,col,buddy.email)
>   col += 1
>   sh.write(row,col,buddy.phone)
>   col +=1
> sh.write(row,col,buddy.comment)
>   row +=1
>  bk.save('C:\Users\owner\Desktop\DEL\tester.xls')
>
> then got this error:
>
> Exception: Attempt to overwrite cell: sheetname=u'names' rowx=0 colx=0
>
> Kindly help with a hint please.
>
> Regards
>
> --
> Elegbede Muhammed Dipo
> TimbaObjects Co.,
> +2348033299270
> +2348077682428
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to