Dropping down a colum in Excel with xlsxwriter

Viewed 60

I'm looking to be able to run the script everytime I get home from work and just add in how much hours I worked and the pay and have it save in a new line each time in Excel but it just keeps writing over the last value I put in. I'm using xlsxwriter.

import xlsxwriter

hours = input('|         Hours worked >>')
HWadge = input('|          Hourly pay >> £')

hours = int(hours)
Hwadge = int(HWadge)
total = hours * Hwadge

print('Money for shift = ','*', total, '*')

workbook = xlsxwriter.Workbook('Pay.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': True})

my_list = [hours, Hwadge, total,]

# How do I make it so I can add a value each day and it just drop down a column?

I'm looking for this output:

screenshot

1 Answers

You can use openpyxl to edit an excel sheet:

from openpyxl import load_workbook, Workbook

hours = input('|         Hours worked >>')
HWadge = input('|          Hourly pay >> £')

hours = int(hours)
Hwadge = int(HWadge)
total = hours * Hwadge
try:
    wb = load_workbook("possible_answer.xlsx") # loading existing workbook
except FileNotFoundError: # of if the excel file is doesn't exist
    wb = Workbook()
ws = wb.active
date = ("date", "08/02/2022")
hours_col = ("hours", f"${hours}")
Hwadge_col = ("Hwadge", f"${Hwadge}")
total_col = ("total", f"${total}")
my_list = [date, hours_col, Hwadge_col, total_col, ]
for row in my_list:
    ws.append(row)
ws.append([" "])

wb.save("possible_answer.xlsx")
Related