...

/

Interacting with Excel and Finishing the Program

Interacting with Excel and Finishing the Program

Interact with Excel and complete the program.

Learning about Openpyxl

Lastly, you need to interact with Excel via the Openpyxl library. Below is sample code for interacting with a standard Excel workbook:

Press + to interact
import openpyxl
workbook = openpyxl.Workbook()
worksheet = workbook.active # or worksheet = workbook['sheet1']
# directly assign a cell a value
worksheet["A1"] = 'Start Here'
# or
worksheet.cell(row=1, column=1, value="Start Here")
# get the value of a cell
x = worksheet["A1"].value
# or
x = worksheet.cell(row=1, column=1).value

The functionality to assign a value to a cell by the exact cell name (A1) or by its row and column coordinates is a handy way to iterate through a list and dynamically assign the row and column with the for loop counter variables. With a double nested for loop, the outer for loop can keep track of the row number, and the inner for loop can keep track of the column number and keep ...