Search⌘ K

Interacting with Excel and Finishing the Program

Explore how to use the Openpyxl library to interact with Excel workbooks in Python. Learn to dynamically assign values to cells using nested loops, handle file paths correctly, and save scraped data into spreadsheets. Understand how to create adaptable programs for engineering data management that automatically update Excel files.

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:

Python 3.8
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 ...