Reputation: 141
Hello I want to write some of my data from my program to a spreadsheet. I am running python3 and have installed xlsxwriter package.
But totally new to xlsxwriter
Here is my dict which I want to write as spreadsheet.
[
{
'name': 'xyz',
'data': [
{
'price': '$29.95', 'service': 'website'
},
{
'price': '$7.08', 'service': 'network'
}
],
'total': '$37.03',
'start_date': '2020-12-01',
'end_date': '2020-12-01'
},
{
'name': 'test',
'data': [
{
'price': '$112.43', 'service': 'website'
},
{
'price': '$188.82', 'service': 'network'
}
],
'total': '$301.25',
'start_date': '2020-12-01',
'end_date': '2020-12-01'
}
]
and here is the output that I want.
Would you please help me how I can do this xlsxwriter
Upvotes: 0
Views: 87
Reputation: 2859
You can make rows based on the content you want, and then iterate over them to write to the document. The following code makes a file called xlsxwriter_test.xlsx
. Then creates a tuple with the rows. It then iterates over the rows, writes to the document, then closes it.
import xlsxwriter
workbook = xlsxwriter.Workbook("xlsxwriter_test.xlsx")
worksheet = workbook.add_worksheet()
rows = (
["Client Name", "Service", "Price", "Start Date", "End Date", "Total"],
["xyz", "website", "$29.95", "12/1/20", "12/31/20", "37.03"],
[None, "network", "$7.08", "12/1/20", "12/31/20", None],
[None,None,None,None,None,None],
["test", "website", "$112.43", "12/1/20", "12/31/20", "301.25"],
[None, "network", "$188.82", "12/1/20", "12/31/20", None]
)
row = 0
for name, service, price, startD, endD,total in (rows):
col = 0
worksheet.write(row,col, name)
col += 1
worksheet.write(row,col, service)
col += 1
worksheet.write(row,col, price)
col += 1
worksheet.write(row,col, startD)
col += 1
worksheet.write(row,col, endD)
col += 1
worksheet.write(row,col, total)
row+=1
workbook.close()
Upvotes: 1