Work Automation – Writing data to an Excel Spreadsheet

Using to the Openpyxl library we will be writing data to an Excel spreadsheet, the data in this example is that taken via input.

We start by defining our workbook and sheet which are pre-existing.

wb = openpyxl.load_workbook(FilePath)
sheet = wb["Sheet1"]

I have introduced a Count variable which is incremented by 1 each time input is entered, this is used to reference the row in which we will be writing to. The column is fixed to 1 i.e. the first column within the spreadsheet and the input, in this case the Name is applied. We then save the spreadsheet before looping back round.

Count = 1
while 1 == 1:
    Name = input("Please enter your name... ")
    Count = Count + 1
    sheet.cell(row=Count, column=1).value = Name
    wb.save(FilePath)

The full source code for this project can be found below:

import openpyxl

wb = openpyxl.load_workbook(FilePath)
sheet = wb["Sheet1"]

Count = 1
while 1 == 1:
    Name = input("Please enter your name... ")
    Count = Count + 1
    sheet.cell(row=Count, column=1).value = Name
    wb.save(FilePath)

Leave a Reply