Search code examples
pythonexcelxlsx

Try to write an excel sheet


I'm trying to write these cookie sales into an excel sheet, but I'm getting the "invalid syntax" error. Any thoughts on how to do this properly? Here is my code:

import xlsxwriter

workbook = xlsxwriter.Workbook('hello2.xlsx')
worksheet = workbook.add_worksheet()

cookie_sales = {
['Name': 'Sue', 'Feeny', 'Jimmy', 'Seymour'. 'Shawn', 'Corey', 'Anne', 'Terry', 'Derek', 'Sammy', 'Topanga', 'Alan', 'Mark', 'Joe', 'Elliot' ],
['Age':  23, 99, 23, 11, 31, 55, 35, 44, 71, 41, 39, 22, 16, 25, 32 ],
['Boxes_Sold':  5234, 5000, 4566, 3344, 3123, 534, 335, 654, 7773, 990, 11, 46, 742, 222, 9],
['Flavor': 'Chocolate', 'Cookie Dough', 'Cookie Dough', 'Mint', 'Vanilla', 'Chocolate', 'Vanilla', 'Strawberry', 'Strawberry', 'Vanilla', 'Mint', 'Strawberry', 'Chocolate', 'Chocolate' 'Mint' ],
}

row = 0
col = 0

for name, age, boxes_sold, flavor in (cookie_sales):
worksheet.write(row, col,     name)
worksheet.write(row, col + 1, age)
worksheet.write(row, col + 2, boxes_sold)
worksheet.write(row, col + 3, flavor)
row += 1


workbook.close()

Solution

  • Not really sure if you're really going for a dictionary here or a list. Changing it all to lists, this should yield the results you're looking for:

    import xlsxwriter
    
    workbook = xlsxwriter.Workbook('hello2.xlsx')
    worksheet = workbook.add_worksheet()
    
    cookie_sales = [
    ['Name', 'Sue', 'Feeny', 'Jimmy', 'Seymour', 'Shawn', 'Corey', 'Anne', 'Terry', 'Derek', 'Sammy', 'Topanga', 'Alan', 'Mark', 'Joe', 'Elliot' ],
    ['Age', 23, 99, 23, 11, 31, 55, 35, 44, 71, 41, 39, 22, 16, 25, 32],
    ['Boxes_Sold', 5234, 5000, 4566, 3344, 3123, 534, 335, 654, 7773, 990, 11, 46, 742, 222, 9],
    ['Flavor', 'Chocolate', 'Cookie Dough', 'Cookie Dough', 'Mint', 'Vanilla', 'Chocolate', 'Vanilla', 'Strawberry', 'Strawberry', 'Vanilla', 'Mint', 'Strawberry', 'Chocolate', 'Chocolate', 'Mint' ]]
    
    row = 0
    col = 0
    
    for name in cookie_sales[0]:
        worksheet.write(row, col, name)
        worksheet.write(row, col + 1, cookie_sales[1][row])
        worksheet.write(row, col + 2, cookie_sales[2][row])
        worksheet.write(row, col + 3, cookie_sales[3][row])
        row += 1
    
    
    workbook.close()
    

    If you're curious about dictionaries, to do it using a dictionary, this would work as well, using your current structure:

    import xlsxwriter
    
    workbook = xlsxwriter.Workbook('hello1.xlsx')
    worksheet = workbook.add_worksheet()
    
    cookie_sales = {
    'Name' : ['Sue', 'Feeny', 'Jimmy', 'Seymour', 'Shawn', 'Corey', 'Anne', 'Terry', 'Derek', 'Sammy', 'Topanga', 'Alan', 'Mark', 'Joe', 'Elliot' ],
    'Age' : [23, 99, 23, 11, 31, 55, 35, 44, 71, 41, 39, 22, 16, 25, 32],
    'Boxes_Sold' : [5234, 5000, 4566, 3344, 3123, 534, 335, 654, 7773, 990, 11, 46, 742, 222, 9],
    'Flavor' : ['Chocolate', 'Cookie Dough', 'Cookie Dough', 'Mint', 'Vanilla', 'Chocolate', 'Vanilla', 'Strawberry', 'Strawberry', 'Vanilla', 'Mint', 'Strawberry', 'Chocolate', 'Chocolate', 'Mint' ]}
    
    row = 0
    col = 0
    
    for key in cookie_sales.keys():
        worksheet.write(0, col, key) # Writes Heading
        row = 1
        for item in cookie_sales[key]: # Writes Heading contents
            worksheet.write(row, col, item)
            row += 1
        col += 1
    
    workbook.close()