when use this showing the 200 response but the excel file was not generated in my file so help me to fix it
@api_view (['POST'])
-- @permission_classes([IsAuthenticated])
def export_combined_template(request):
# Create an in-memory output buffer
output = io.BytesIO()
# Get DataFrames from each export function
df_contractor = export_contractor_excel_sheet_template()
df_applicability = export_example_applicability_template()
df_incharge = export_incharge_owner_template()
# Optional: Log DataFrame shapes to verify data is present
print("Contractor DF shape:", df_contractor.shape)
print("Applicability DF shape:", df_applicability.shape)
print("Incharge DF shape:", df_incharge.shape)
# Use ExcelWriter to combine multiple sheets into a single Excel file
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
# Write Contractor Template sheet
df_contractor.to_excel(writer, sheet_name='Contractor Template', index=False)
contractor_ws = writer.sheets['Contractor Template']
header_format = writer.book.add_format({'bold': True, 'align': 'center', 'valign': 'vcenter'})
for col_num, header in enumerate(df_contractor.columns):
contractor_ws.write(0, col_num, header, header_format)
contractor_ws.set_column(col_num, col_num, 18)
# Add dropdown validation for "Is Sub Contractor" (column index 4)
contractor_ws.data_validation(1, 4, 1048575, 4, {
'validate': 'list',
'source': ['Yes', 'No'],
'ignore_blank': True
})
# Write Applicability Template sheet
df_applicability.to_excel(writer, sheet_name='Applicability Template', index=False)
applicability_ws = writer.sheets['Applicability Template']
for col_num, header in enumerate(df_applicability.columns):
applicability_ws.write(0, col_num, header, header_format)
applicability_ws.set_column(col_num, col_num, 18)
# Add dropdown validation for "Is Enable" (column index 2)
applicability_ws.data_validation(1, 2, 1048575, 2, {
'validate': 'list',
'source': ['Yes', 'No'],
'ignore_blank': True
})
# Write Incharge Owner Template sheet
df_incharge.to_excel(writer, sheet_name='Incharge Owner Template', index=False)
incharge_ws = writer.sheets['Incharge Owner Template']
for col_num, header in enumerate(df_incharge.columns):
incharge_ws.write(0, col_num, header, header_format)
incharge_ws.set_column(col_num, col_num, 18)
# (Add additional formatting or validations for the incharge sheet if needed)
# Rewind the buffer to the beginning
output.seek(0)
# Build the HTTP response with the Excel file
response = HttpResponse(
output.read(),
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
response['Content-Disposition'] = 'attachment; filename="combined_templates.xlsx"'
return response
def export_combined_template(request):
# Create an in-memory output buffer
output = io.BytesIO()
# Get DataFrames from each export function
df_contractor = export_contractor_excel_sheet_template()
df_applicability = export_example_applicability_template()
df_incharge = export_incharge_owner_template()
# Optional: Log DataFrame shapes to verify data is present
print("Contractor DF shape:", df_contractor.shape)
print("Applicability DF shape:", df_applicability.shape)
print("Incharge DF shape:", df_incharge.shape)
# Use ExcelWriter to combine multiple sheets into a single Excel file
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
# Write Contractor Template sheet
df_contractor.to_excel(writer, sheet_name='Contractor Template', index=False)
contractor_ws = writer.sheets['Contractor Template']
header_format = writer.book.add_format({'bold': True, 'align': 'center', 'valign': 'vcenter'})
for col_num, header in enumerate(df_contractor.columns):
contractor_ws.write(0, col_num, header, header_format)
contractor_ws.set_column(col_num, col_num, 18)
# Add dropdown validation for "Is Sub Contractor" (column index 4)
contractor_ws.data_validation(1, 4, 1048575, 4, {
'validate': 'list',
'source': ['Yes', 'No'],
'ignore_blank': True
})
# Write Applicability Template sheet
df_applicability.to_excel(writer, sheet_name='Applicability Template', index=False)
applicability_ws = writer.sheets['Applicability Template']
for col_num, header in enumerate(df_applicability.columns):
applicability_ws.write(0, col_num, header, header_format)
applicability_ws.set_column(col_num, col_num, 18)
# Add dropdown validation for "Is Enable" (column index 2)
applicability_ws.data_validation(1, 2, 1048575, 2, {
'validate': 'list',
'source': ['Yes', 'No'],
'ignore_blank': True
})
# Write Incharge Owner Template sheet
df_incharge.to_excel(writer, sheet_name='Incharge Owner Template', index=False)
incharge_ws = writer.sheets['Incharge Owner Template']
for col_num, header in enumerate(df_incharge.columns):
incharge_ws.write(0, col_num, header, header_format)
incharge_ws.set_column(col_num, col_num, 18)
# (Add additional formatting or validations for the incharge sheet if needed)
# Rewind the buffer to the beginning
output.seek(0)
# Build the HTTP response with the Excel file
response = HttpResponse(
output.read(),
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
response['Content-Disposition'] = 'attachment; filename="combined_templates.xlsx"'
return response
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+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/9211a987-fd5a-4089-8678-0293ef3b4e24n%40googlegroups.com.