How to get data from django model to excel sheet using openpyxl including images.

991 views
Skip to first unread message

Ashutosh Mishra

unread,
Jul 9, 2020, 11:01:23 PM7/9/20
to Django users
I am creating an api to get data and images from django model to excel sheet ,how can i do that,someone please help me.

o1bigtenor

unread,
Jul 10, 2020, 8:18:36 AM7/10/20
to django...@googlegroups.com
On Thu, Jul 9, 2020 at 10:02 PM Ashutosh Mishra <ashutosh...@gmail.com> wrote:
I am creating an api to get data and images from django model to excel sheet ,how can i do that,someone please help me. 


What have you tried so far?

Regards 

Ashutosh Mishra

unread,
Jul 10, 2020, 9:00:22 AM7/10/20
to Django users
i have created an api through which i can download the excel file with all fields except images.I am posting my models and views

models.py

class Task(models.Model):

Id=models.IntegerField()
Name=models.CharField(max_length=50,null=False,blank=True)
Image1=models.FileField(blank=True, default="",
upload_to="media/images",null=True)
Image2=models.FileField(blank=True, default="",
upload_to="media/images",null=True)
Date=models.DateField(null=True,blank=True)
def __str__(self):
return str(self.Name)

views.py
class TaskViewSet(viewsets.ViewSet):

def list(self,request):
try:
queryset=Task.objects.all()
response = HttpResponse(content_type='application/ms-excel')
#response=HttpResponse(content_type='application/ms-excel')

#response['Content-Disposition'] = 'attachment; filename="users.xls'
response['Content-Disposition']='attachment; filename="users.xls"'

wb=openpyxl.Workbook()
ws=wb.active

row_num=0
columns=['Id','Name','Image1','Image2','Date']

for col_num in range(len(columns)):
c = ws.cell(row=row_num + 1, column=col_num + 1)
c.value = columns[col_num]
for obj in queryset:
row_num+=1
row = [
obj.Id,
obj.Name,
obj.Image1.url,
obj.Image2.url,
str(obj.Date),
]
print(type(row))

for col_num in range(len(row)):
c = ws.cell(row=row_num + 1, column=col_num + 1)
c.value = row[col_num]

wb.save(response)
return response
except Exception as error:
traceback.print_exc()
return Response({"message": str(error), "success": False}, status=status.HTTP_200_OK)


Vishesh Mangla

unread,
Jul 10, 2020, 9:45:26 AM7/10/20
to django...@googlegroups.com

Api’s are better suited for Django rest framework.

 

Sent from Mail for Windows 10

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c1092b58-9d91-4d9c-8654-54df883c5172n%40googlegroups.com.

 

Ashutosh Mishra

unread,
Jul 10, 2020, 9:54:52 AM7/10/20
to django...@googlegroups.com
I am doing that using openpyxl
But not getting the image in excel sheet


See this

VenkataSivaRamiReddy

unread,
Jul 10, 2020, 1:39:49 PM7/10/20
to django...@googlegroups.com
Hi man,

Have you tried *xlsxwditer* package.

If not please check

Reply all
Reply to author
Forward
0 new messages