My requirement is to write into a sample excel (static) file, and download the copied static file in the device. The copy is required cause the sample file contains some formatting, which I need in the downloaded file as well.
Directory Structure : Django Project - project - app - static - files -> sample.xlsx - css - js - manage.py
Now, I need to create a copy of sample.xlsx file, write some details in it and download it.
I've tried below code, in which I am creating a new file and copying the content from sample.xlsx to new_file.xlsx . But it does not copy the formatting from sample.xlsx
def exportXLS(request):
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename="new_file.xlsx"'
sample = load_workbook(sample_file_path) #opening sample.xlsx
active_sheet = sample.worksheets[0]
new_workbook = Workbook() #creating new excel file
worksheet = wb.active
worksheet.title = "Sheet Name"
mr = active_sheet.max_row
mc = active_sheet.max_column
#copying values from sample.xlsx to new_file.xlsx
for i in range (1, mr + 1):
for j in range (1, mc + 1):
c = active_sheet.cell(row = i, column = j)
worksheet.cell(row = i, column = j).value = c.value
new_workbook.save(response)
return response
==> I would like to make a COPY of sample.xlsx first then write the details in the copy, then download it.