| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2013 Simplistix Ltd |
| 3 |
# |
| 4 |
# This Software is released under the MIT License: |
| 5 |
# http://www.opensource.org/licenses/mit-license.html |
| 6 |
# See license.txt for more details. |
| 7 |
|
| 8 |
|
| 9 |
from datetime import datetime, time |
| 10 |
from petl.compat import xrange |
| 11 |
|
| 12 |
|
| 13 |
class Index(object): |
| 14 |
def __init__(self, name): |
| 15 |
self.name = name |
| 16 |
|
| 17 |
|
| 18 |
class Row(Index): |
| 19 |
""" |
| 20 |
A one-based, end-inclusive row index for use in slices, |
| 21 |
eg:: ``[Row(1):Row(2), :]`` |
| 22 |
""" |
| 23 |
def __index__(self): |
| 24 |
return int(self.name) - 1 |
| 25 |
|
| 26 |
|
| 27 |
class Col(Index): |
| 28 |
""" |
| 29 |
An end-inclusive column label index for use in slices, |
| 30 |
eg: ``[:, Col('A'), Col('B')]`` |
| 31 |
""" |
| 32 |
def __index__(self): |
| 33 |
from xlwt.Utils import col_by_name |
| 34 |
return col_by_name(self.name) |
| 35 |
|
| 36 |
|
| 37 |
class SheetView(object): |
| 38 |
""" |
| 39 |
A view on a sheet in a workbook. Should be created by indexing a |
| 40 |
:class:`View`. |
| 41 |
|
| 42 |
These can be sliced to create smaller views. |
| 43 |
|
| 44 |
Views can be iterated over to return a set of iterables, one for each row |
| 45 |
in the view. Data is returned as in the cell values with the exception of |
| 46 |
dates and times which are converted into :class:`~datetime.datetime` |
| 47 |
instances. |
| 48 |
""" |
| 49 |
|
| 50 |
def __init__(self, book, sheet, row_slice=None, col_slice=None): |
| 51 |
#: The workbook used by this view. |
| 52 |
self.book = book |
| 53 |
#: The sheet in the workbook used by this view. |
| 54 |
self.sheet = sheet |
| 55 |
for name, source in (('rows', row_slice), ('cols', col_slice)): |
| 56 |
start = 0 |
| 57 |
stop = max_n = getattr(self.sheet, 'n'+name) |
| 58 |
if isinstance(source, slice): |
| 59 |
if source.start is not None: |
| 60 |
start_val = source.start |
| 61 |
if isinstance(start_val, Index): |
| 62 |
start_val = start_val.__index__() |
| 63 |
if start_val < 0: |
| 64 |
start = max(0, max_n + start_val) |
| 65 |
elif start_val > 0: |
| 66 |
start = min(max_n, start_val) |
| 67 |
if source.stop is not None: |
| 68 |
stop_val = source.stop |
| 69 |
if isinstance(stop_val, Index): |
| 70 |
stop_val = stop_val.__index__() + 1 |
| 71 |
if stop_val < 0: |
| 72 |
stop = max(0, max_n + stop_val) |
| 73 |
elif stop_val > 0: |
| 74 |
stop = min(max_n, stop_val) |
| 75 |
setattr(self, name, xrange(start, stop)) |
| 76 |
|
| 77 |
def __row(self, rowx): |
| 78 |
from xlrd import XL_CELL_DATE, xldate_as_tuple |
| 79 |
for colx in self.cols: |
| 80 |
value = self.sheet.cell_value(rowx, colx) |
| 81 |
if self.sheet.cell_type(rowx, colx) == XL_CELL_DATE: |
| 82 |
date_parts = xldate_as_tuple(value, self.book.datemode) |
| 83 |
# Times come out with a year of 0. |
| 84 |
if date_parts[0]: |
| 85 |
value = datetime(*date_parts) |
| 86 |
else: |
| 87 |
value = time(*date_parts[3:]) |
| 88 |
yield value |
| 89 |
|
| 90 |
def __iter__(self): |
| 91 |
for rowx in self.rows: |
| 92 |
yield self.__row(rowx) |
| 93 |
|
| 94 |
def __getitem__(self, slices): |
| 95 |
assert isinstance(slices, tuple) |
| 96 |
assert len(slices) == 2 |
| 97 |
return self.__class__(self.book, self.sheet, *slices) |
| 98 |
|
| 99 |
|
| 100 |
class View(object): |
| 101 |
""" |
| 102 |
A view wrapper around a :class:`~xlrd.Book` that allows for easy |
| 103 |
iteration over the data in a group of cells. |
| 104 |
|
| 105 |
:param path: The path of the .xls from which to create views. |
| 106 |
:param class_: An class to use instead of :class:`SheetView` for views of |
| 107 |
sheets. |
| 108 |
""" |
| 109 |
|
| 110 |
#: This can be replaced in a sub-class to use something other than |
| 111 |
#: :class:`SheetView` for the views of sheets returned. |
| 112 |
class_ = SheetView |
| 113 |
|
| 114 |
def __init__(self, file_contents, class_=None, **kwargs): |
| 115 |
self.class_ = class_ or self.class_ |
| 116 |
from xlrd import open_workbook |
| 117 |
self.book = open_workbook(file_contents=file_contents, |
| 118 |
on_demand=True, **kwargs) |
| 119 |
|
| 120 |
def __getitem__(self, item): |
| 121 |
""" |
| 122 |
Returns of a view of a sheet in the workbook this view is created for. |
| 123 |
|
| 124 |
:param item: either zero-based integer index or a sheet name. |
| 125 |
""" |
| 126 |
if isinstance(item, int): |
| 127 |
sheet = self.book.sheet_by_index(item) |
| 128 |
else: |
| 129 |
sheet = self.book.sheet_by_name(item) |
| 130 |
return self.class_(self.book, sheet) |