I've not used this site before, I would like to get opinions on the code I've completed for my first 'program' in Python. I would like to understand how 'good' it is and where it can be improved, anything that says "you're clearly a beginner and haven't learned xxx" would be helpful.
Background to project: My payslips are in a weird format with info dotted around all over a spreadsheet and have slight variations in positioning month on month. For example the value of a field might not be in the adjacent cell it might be below it for one field and to the right for another (although this relative positioning is consistent month on month) I combined manually all the months into one Excel and wanted to structure the data so I could eventually run calculations on it to ensure it was correct and my taxes were correct (still need to do that part).
# -*- coding: utf-8 -*-
import openpyxl
def get_data(ws):
data = []
for y in range(1,ws.max_row + 1):
rows = []
for x in range(1,ws.max_column + 1):
rows.append(ws.cell(column = x, row = y).value)
data.append(rows)
return data
def search_data(data, criteria):
location = []
for row in range(len(data)):
for column in range(len(data[row])):
if data[row][column] == criteria:
location.append([row,column])
if len(location) == 0:
print('Not present on sheet')
return None
return location
def offset(location,rows,columns):
location[0] = location[0] + rows
location[1] = location[1] + columns
return location
def get_table(data, startloc, columns=None):
count = 1
rows = 1
table = []
temprow = startloc[0]
tempcol = startloc[1]
while count != 0:
if data[temprow][tempcol] != None:
temprow = temprow + 1
if temprow == len(data):
break
else:
count = 0
rows = temprow - startloc[0]
if columns == None:
temprow = startloc[0]
tempcol = startloc[1]
count = 1
while count != 0:
if data[temprow][tempcol] != None:
tempcol = tempcol + 1
if tempcol == len(data[temprow]):
break
else:
count = 0
columns = tempcol - startloc[1]
for y in range(rows):
table_rows = []
for x in range(columns):
table_rows.append(data[startloc[0]+y][startloc[1]+x])
table.append(table_rows)
return table
def get_value(data, location):
return data[location[0]][location[1]]
def get_tax_info(ws):
data = get_data(ws)
tax_info = []
#gets individual values
tax_period = get_value(data,offset(search_data(data,'Calendar Month')[0],0,1))
tax_period_ending = get_value(data,offset(search_data(data,'Calendar Month')[0],0,2))
tax_code = get_value(data,offset(search_data(data, 'Tax Code')[0],1,0))
tax_basis = get_value(data,offset(search_data(data, 'Tax Basis')[0],1,0))
NI_category = get_value(data,offset(search_data(data,'NI Category')[0],1,0))
total_payments = get_value(data,offset(search_data(data,'Total Payments')[0],1,0))
net_pay = get_value(data,offset(search_data(data,'Net Pay')[0],1,0))
#gets tables
payments = get_table(data,offset(search_data(data,'Payments')[0],4,0))
deductions = get_table(data,offset(search_data(data,'Deductions')[0],4,0))
balances = get_table(data,offset(search_data(data,'Balances')[0],4,0))
#combine
tax_info.extend([tax_period, tax_period_ending, tax_code, tax_basis, NI_category, total_payments, net_pay])
tax_info.append(payments)
tax_info.append(deductions)
tax_info.append(balances)
return tax_info
class payslip:
def __init__(self, tax_period, tax_period_ending, tax_code, tax_basis, NI_category, total_payments, net_pay, payments, deductions, balances):
'Creates a payslip object with all the fields of the payslip'
self.tax_period_ending = tax_period_ending
self.tax_code = tax_code
self.tax_basis = tax_basis
self.NI_category = NI_category
self.total_payments = total_payments
self.net_pay = net_pay
self.payments = payments
self.deductions = deductions
self.balances = balances
wb = openpyxl.load_workbook('.../Payslips.xlsx')
sheet_names = wb.sheetnames
payslips = []
for i in range(0,len(sheet_names)):
each = sheet_names[i]
ws = wb[each]
tax_info = get_tax_info(ws)
payslips.append(payslip(tax_info[0], tax_info[1], tax_info[2], tax_info[3], tax_info[4], tax_info[5], tax_info[6], tax_info[7],tax_info[8],tax_info[9]))
wb2 = openpyxl.Workbook()
ws2 = wb2.active
ws2.title = 'Payslips Summary'
ws2['A1'],ws2['B1'],ws2['C1'],ws2['D1'],ws2['E1'], ws2['F1'] = 'tax_period_ending', 'total_payments', 'net_pay', 'tax_code', 'NI_category', 'tax_basis'
for i in range(0,len(payslips)):
ws2.cell(row=i+2, column=1).value = payslips[i].tax_period_ending
ws2.cell(row=i+2, column=2).value = payslips[i].total_payments
ws2.cell(row=i+2, column=3).value = payslips[i].net_pay
ws2.cell(row=i+2, column=4).value = payslips[i].tax_code
ws2.cell(row=i+2, column=5).value = payslips[i].NI_category
ws2.cell(row=i+2, column=6).value = payslips[i].tax_basis
wb2.save('Payslips_Summary.xlsx')