Multiplication Table Maker

Create a program multiplicationTable.py that takes a number N from the command line and creates an N×N multiplication table in an Excel spreadsheet.

import sys

import openpyxl

from openpyxl.styles import Font

if len(sys.argv) != 2:

  print(“Usage: python multiplicationTable.py <N>”)

else:

  n = int(sys.argv[1])

  # Create a new workbook and select the active sheet

  wb = openpyxl.Workbook()

  sheet = wb.active

  # Add bold labels to the first row and column

  for i in range(1, n + 1):

    sheet.cell(row=1, column=i + 1).value = i

    sheet.cell(row=1, column=i + 1).font = Font(bold=True)

    sheet.cell(row=i + 1, column=1).value = i

    sheet.cell(row=i + 1, column=1).font = Font(bold=True)

  # Fill in the multiplication table

  for i in range(1, n + 1):

    for j in range(1, n + 1):

      sheet.cell(row=i + 1, column=j + 1).value = i * j

  # Save the workbook

  wb.save(f’multiplication_table_{n}x{n}.xlsx’)

Leave a comment

Your email address will not be published. Required fields are marked *