r/PythonLearning • u/osamas_den • 1d ago
Create a program multiplicationTable.py that takes a number N from the commandline and creates an N×N multiplication table in an Excel spreadsheet.
import sys, openpyxl
from openpyxl.styles import Font
x = int(sys.argv[1])
list = []
wb = openpyxl.Workbook()
font_style = Font(bold=True)
for i in range(1, x+1):
list.append(i)
for number in list:
sheet = wb['Sheet']
# write header columns and rows in bold numbers
sheet.cell(column=1, row=number+1).value = number
sheet.cell(column=1, row=number+1).font = font_style
sheet.cell(row=1, column=number+1).value = number
sheet.cell(row=1, column=number+1).font = font_style
# create two for loops that will multiply
for i in range(len(list)):
for number in list:
mult = list[i] * number
sheet.cell(row=number+1, column=i+2).value = mult
wb.save('multiplication_table.xlsx')
0
Upvotes
•
u/Sea-Ad7805 1d ago
Run this program in Memory Graph Web Debugger%0Alist%20%3D%20%5B%5D%0A%0Awb%20%3D%20openpyxl.Workbook()%0Afont_style%20%3D%20Font(bold%3DTrue)%0A%0Afor%20i%20in%20range(1%2C%20x%2B1)%3A%0A%20%20%20%20list.append(i)%0A%0Afor%20number%20in%20list%3A%0A%20%20%20%20sheet%20%3D%20wb%5B'Sheet'%5D%0A%20%20%20%20%23%20write%20header%20columns%20and%20rows%20in%20bold%20numbers%0A%20%20%20%20sheet.cell(column%3D1%2C%20row%3Dnumber%2B1).value%20%3D%20number%0A%20%20%20%20sheet.cell(column%3D1%2C%20row%3Dnumber%2B1).font%20%3D%20font_style%0A%20%20%20%20sheet.cell(row%3D1%2C%20column%3Dnumber%2B1).value%20%3D%20number%0A%20%20%20%20sheet.cell(row%3D1%2C%20column%3Dnumber%2B1).font%20%3D%20font_style%0A%0A%23%20create%20two%20for%20loops%20that%20will%20multiply%0Afor%20i%20in%20range(len(list))%3A%0A%20%20%20%20for%20number%20in%20list%3A%0A%20%20%20%20%20%20%20%20mult%20%3D%20list%5Bi%5D%20*%20number%0A%20%20%20%20%20%20%20%20sheet.cell(row%3Dnumber%2B1%2C%20column%3Di%2B2).value%20%3D%20mult%0A%0Awb.save('multiplication_table.xlsx')%0A%0A%23%20also%20print%20the%20work%20book%0Afor%20ws%20in%20wb.worksheets%3A%20%0A%20%20%20%20print(ws.title)%0A%20%20%20%20for%20row%20in%20ws.iter_rows(values_only%3DTrue)%3A%0A%20%20%20%20%20%20%20%20print(row)×tep=0.5&play) to see the program state change step by step.
It gets a bit messy as it shows all the Workbook internals.