Skip to content

Instantly share code, notes, and snippets.

@armaandhir
Last active August 6, 2024 04:23
Show Gist options
  • Save armaandhir/455828396bf5c18256da to your computer and use it in GitHub Desktop.
Save armaandhir/455828396bf5c18256da to your computer and use it in GitHub Desktop.
Reading data from excel using openpyxl
#
# I hate the documentation of openpyxl and it took me a while to undertand their stuff. So I decided to write down this code.
# Has some wrapper functions that reads all rows from the excel sheet and also a function to read a particular row.
# Add some code to the functions if you wish to do something on fly like adding values to list and sorting them later.
#
# Date: 28/09/2015
from openpyxl import load_workbook
# Reads all rows
def read_all_rows(workSheet):
# You need to set an offset if the first row is your column headings
for row in workSheet.iter_rows(row_offset=1):
print "\n"
for cell in row:
print cell.value
# Prints the data in a particular column
def read_column(workSheet, columnNumber):
# You need to set an offset if the first row is your column headings
for row in workSheet.iter_rows(row_offset=1):
print row[columnNumber].value
# ------------------------------ MAIN ---------------------------------------
workBook = load_workbook(filename='errortype.xlsx', read_only=True, use_iterators=True)
workSheet = workBook['Sheet1']
#read_all_rows(workSheet)
read_column(workSheet, 0)
@sanjaykrishnan
Copy link

@armaandhir by setting the rowoffset to 1, it prints an extra row containing null values..do you know why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment