Skip to content

Instantly share code, notes, and snippets.

@Cilyan
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cilyan/8809722 to your computer and use it in GitHub Desktop.
Save Cilyan/8809722 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of the nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import csv
import openpyxl
class XlsxReader:
"""
Wrapper class that reads an xlsx file like a :py:class:`csv.reader`.
It takes as argument the file path to the Excel file and the name of
the worksheet to work on. The interface is the same as for
:py:class:`csv.reader`, this is an iterator that yields a list of
strings per line.
"""
def __init__(self, filename, worksheet):
self.filename = filename
self.workbook = openpyxl.load_workbook(filename, use_iterators = True)
self.worksheet = self.workbook.get_sheet_by_name(worksheet)
if self.worksheet is None:
raise RuntimeError("Worksheet {} not found".format(worksheet))
self.line_num = 0
self.iter_rows = self.worksheet.iter_rows()
def __iter__(self):
return self
def __next__(self):
row = next(self.iter_rows)
self.line_num += 1
return [ cell.internal_value for cell in row ]
class DictXlsxReader(csv.DictReader):
"""
Wrapper class that reads an xlsx file like a :py:class:`csv.DictReader`.
It takes as argument the file path to the Excel file and the name of
the worksheet to work on. The interface is the same as for
:py:class:`csv.DictReader`, this is an iterator that yields a dict of
strings per line. See :py:class:`csv.DictReader` for the meaning of
``fieldnames``, ``restkey`` and ``restval``.
"""
def __init__(
self, filename, worksheet, fieldnames=None,
restkey=None, restval=None
):
self._fieldnames = fieldnames
self.restkey = restkey
self.restval = restval
self.reader = XlsxReader(filename, worksheet)
self.line_num = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment