Skip to content

Instantly share code, notes, and snippets.

@PhanDuc
Forked from armaandhir/readExcel.py
Created August 1, 2018 11:40
Show Gist options
  • Save PhanDuc/2654582c8259b2a917630dceb4f72341 to your computer and use it in GitHub Desktop.
Save PhanDuc/2654582c8259b2a917630dceb4f72341 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment