Easier done than said ….like actually

03:35:23 Coffee with a drop of sleep in my eyes


I am rather tired at the moment but have enough energy to solve this task: Multiplication table challenge..

# Usage: python multiplication.py <N>

import sys
import os
import openpyxl
import logging
from openpyxl.styles import Font

logging.basicConfig(level=logging.DEBUG,
                    format="%(asctime)s - %(levelname)s - %(message)s")

if len(sys.argv) == 2:
    limit = int(sys.argv[1])
    filename = os.path.join(".", "multiplication{}.xlsx".format(limit))
    # TODO: create xlsx
    wb = openpyxl.Workbook()
    sheet = wb.get_active_sheet()
    if not sheet:
        wb.create_sheet(title="Multiplication Table")
    sheet = wb.get_active_sheet()
    fontObj = Font(bold=True)
    # TODO:print first row
    topRow = list(range(1, limit + 1))
    topRow = [" "] + topRow
    i = 1
    for val in topRow:
        curr_cell = sheet.cell(row=1, column=i)
        curr_cell.font = fontObj
        curr_cell.value = val
        i += 1
        # create table
        i = 1
        for row in range(1, limit + 1):
            cur = sheet.cell(row=row + 1, column=1)
            cur.font = fontObj
            cur.value = row
            for col in range(1, limit + 1):
                sheet.cell(row=row + 1, column=col+1).value = row * col
                i += 1
    wb.save(filename)
else:
    logging.error("Usage: python multiplication.py <N>")

I expected this to be perplexing but was an enjoyable process.

%d bloggers like this: