Skip to content

Instantly share code, notes, and snippets.

@ricardo-valerio
Last active January 12, 2019 19:42
Show Gist options
  • Save ricardo-valerio/ab274935d31ddb0dff9a3b9f625632bd to your computer and use it in GitHub Desktop.
Save ricardo-valerio/ab274935d31ddb0dff9a3b9f625632bd to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
from openpyxl import load_workbook
workbook_file = load_workbook(filename="test_file_for_data_entry_automation.xlsx",
data_only=True,
read_only=True)
print(workbook_file.sheetnames) # ['Sample-spreadsheet-file', 'Another Sheet']
for sheet in workbook_file:
print(sheet.title, "Dimension:", sheet.calculate_dimension())
# Select a worksheet by name:
selected_worksheet = workbook_file.active
# selected_worksheet = workbook_file['Sample-spreadsheet-file']
print(selected_worksheet.title, "Dimension:", selected_worksheet.calculate_dimension())
# Accessing one cell:
print("What's on Cell 'C9' in the 'selected_worksheet'? ->", selected_worksheet["C9"].value)
print("Another way to get the value on C9 ->", selected_worksheet.cell(column=3, row=9).value)
# ----------------------------------------------------
# Getting ALL the relevant values from a sheet
#-----------------------------------------------------
# for row in selected_worksheet.rows:
# for cell in row:
# print(cell.value, end=" | ")
# print("\n-------")
# ----------------------------------------------------
# exactly the same output as the previous for loop:
# ----------------------------------------------------
# for row in selected_worksheet.values:
# for value in row:
# print(value, end=" | ")
# print("\n-------")
# ----------------------------------------------------
# Getting only the relevant values from selected range
#-----------------------------------------------------
for cell_in_range in selected_worksheet['C8':'I10']:
for cell in cell_in_range:
print(cell.value, type(cell.value), end=" | ")
print("\n-------")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment