Instruction
Well done! Let's summarize what we've learned in this part:
- We can load Excel files into Python using the
load_workbookfunction:wb = load_workbook("workbook_name.xlsx") - To get the names of available worksheets, we can use the
sheetnamesproperty. It returns a list of all worksheet names:wb.sheetnames - We can access a particular worksheet using either the
get_sheet_by_name()method or brackets:wb.get_sheet_by_name("sheetname") wb["sheetname"] - We find out which worksheet is
activeby checking the active property:wb.active - We can access a particular cell in two ways:
- By using the
cellproperty and specifying therowandcolumnparameters:selected_ws.cell(row=1, column=1)
- By using the A1 notation inside brackets:
selected_ws["A1"]
- By using the
- Each cell can provide us with a lot of data:
selected_cell = ws.cell(row=1, column=1) selected_cell.value # the value held in a given cell selected_cell.coordinate # the position of a cell in A1 notation selected_cell.row # the row position of a cell selected_cell.column # the column position of a cell
- We can iterate over the rectangular area of a given worksheet using bracket notation:
for row in worksheet["A1":"D4"]: for cell in row: print(cell.value) - We can also use the
iter_rows()method:for row in worksheet.iter_rows(max_col=..., max_row=...): for cell in row: ...
Okay, time for a quick review!
Exercise
Click to continue.



