perbaikan excel writer

1 parent ef2b6867
......@@ -254,21 +254,44 @@ def csv_response(request, value, filename=None):
return response
def xls_response(request, value, filename=None):
def xls_response(request, value, filename=None, header_format=None):
if not filename:
filename = get_random_string(20)+".xls"
response = request.response
response.content_type = 'application/xls'
response.content_disposition = 'filename=' + filename
from pandas import DataFrame
df = DataFrame(value.get('rows', []), columns=value.get('header', []))
df.to_excel(filename, index=False)
import pandas as pd
df = pd.DataFrame(value.get('rows', []), columns=value.get('header', []))
writer = pd.ExcelWriter(filename, engine='xlsxwriter')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
# Use this if creating multiple sheets, but here we just need one for simplicity
worksheet = writer.sheets
# Add a header format. You can customize properties like fg_color (fill color),
# bold, text_wrap, border, etc.
if header_format is None:
header_format = {
'bold': True,
'text_wrap': True,
'valign': 'top',
'fg_color': '#D7E4BC', # Example color (light green)
'border': 1
}
hformat = workbook.add_format(header_format)
df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False, index=False)
# Write the column headers with the defined format.
for col_num, value in enumerate(df.columns.values):
writer.sheets['Sheet1'].write(0, col_num, value, hformat)
writer.close()
# writer.save()
with open(filename, 'rb') as f:
response.write(f.read())
return response
def terbilang(bil):
angka = ["", "Satu", "Dua", "Tiga", "Empat", "Lima", "Enam",
"Tujuh", "Delapan", "Sembilan", "Sepuluh", "Sebelas"]
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!