python excel Flashcards
Method to open the excel spreadsheet
openpyxl.load_workbook()
Name of module you need to import to work with excel spreadsheets
openpyxl
What parameters does the load_workbook method need?
the file path
How to select first available worksheet
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
What is the integer of the first row & column?
1, 1
How to create a cell object
Using the sheet object’s cell method:
cell_obj = sheet_obj.cell(row = 1, column = 1)
Display the value inside each cell object
print(cell_obj.value)
Get value of total rows & columns
row = sheet_obj.max_row
column = sheet_obj.max_column
Print all values from first column
for i in range(1, row+1):
cell_obj = sheet_obj.cell(row = i, column = 1)
print(cell_obj.value)
print all values from first row
Same as printing columns but you have range(1, column+1) and (row = 1, column = i)
Create a new empty xl file
from openpyxl import Workbook
workbook = Workbook()
workbook.save(filename=”file-name.xlsx”)
assign value to cell object
c1 = sheet.cell(row = 1, column = 1)
c1.value = “Hello”
access a cell object by its actual excel name
c3 = sheet[‘A2’]
A2 means column = 1 & row = 2
append data to sheet
data = ((1, 2, 3),(4, 5, 6))
for row in data:
sheet.append(row)
make cell A7 set to a formula that sums the values in A1, A2, A3, A4, A5
sheet[‘A7’] = ‘= SUM(A1:A5)’