-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathexcel.py
253 lines (227 loc) · 9.87 KB
/
excel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""This module offers a read and write function to get
2-dimensional lists in and out of Excel files.
"""
import re
import itertools
import datetime as dt
# Optional dependencies
try:
import openpyxl
except ImportError:
openpyxl = None
try:
import pyxlsb
except ImportError:
pyxlsb = None
try:
import xlrd
from xlrd.biffh import error_text_from_code
except ImportError:
xlrd = None
try:
import xlwt
except ImportError:
xlwt = None
try:
import xlsxwriter
except ImportError:
xlsxwriter = None
def read(sheet, first_cell="A1", last_cell=None):
"""Read a 2-dimensional list from an Excel range.
Parameters
----------
sheet : object
An xlrd, openpyxl or pyxlsb sheet object
first_cell : str or tuple, optional
Top-left corner of the Excel range you want to read.
Can be a string like "A1" or a row/col tuple like (1, 1),
default is "A1".
last_cell : str or tuple, optional
Bottom-right corner of the Excel range you want to read.
Can be a string like "A1" or a row/col tuple like (1, 1),
default is the bottom-right cell of the used range.
Returns
-------
list
A 2-dimensional list with the values of the Excel range
"""
# xlrd
if xlrd and isinstance(sheet, xlrd.sheet.Sheet):
# isinstance returns True if sheet is of type xlrd.sheet.Sheet
if last_cell is None:
# actual range with data, not used range
last_cell = (sheet.nrows, sheet.ncols)
# Transform "A1" notation into tuples of 1-based indices
if not isinstance(first_cell, tuple):
first_cell = xl_cell_to_rowcol(first_cell)
first_cell = (first_cell[0] + 1, first_cell[1] + 1)
if not isinstance(last_cell, tuple):
last_cell = xl_cell_to_rowcol(last_cell)
last_cell = (last_cell[0] + 1, last_cell[1] + 1)
values = []
for r in range(first_cell[0] - 1, last_cell[0]):
row = []
for c in range(first_cell[1] - 1, last_cell[1]):
# Handle the different cell types
if sheet.cell(r, c).ctype == xlrd.XL_CELL_DATE:
value = xlrd.xldate.xldate_as_datetime(
sheet.cell(r, c).value, sheet.book.datemode)
elif sheet.cell(r, c).ctype in [xlrd.XL_CELL_EMPTY,
xlrd.XL_CELL_BLANK]:
value = None
elif sheet.cell(r, c).ctype == xlrd.XL_CELL_ERROR:
value = error_text_from_code[sheet.cell(r, c).value]
elif sheet.cell(r, c).ctype == xlrd.XL_CELL_BOOLEAN:
value = bool(sheet.cell(r, c).value)
else:
value = sheet.cell(r, c).value
row.append(value)
values.append(row)
return values
# OpenPyXL
elif openpyxl and isinstance(
sheet,
(openpyxl.worksheet.worksheet.Worksheet,
openpyxl.worksheet._read_only.ReadOnlyWorksheet)):
if last_cell is None:
# used range
last_cell = (sheet.max_row, sheet.max_column)
if not isinstance(first_cell, tuple):
first_cell = openpyxl.utils.cell.coordinate_to_tuple(first_cell)
if not isinstance(last_cell, tuple):
last_cell = openpyxl.utils.cell.coordinate_to_tuple(last_cell)
data = []
for row in sheet.iter_rows(min_row=first_cell[0], min_col=first_cell[1],
max_row=last_cell[0], max_col=last_cell[1],
values_only=True):
data.append(list(row))
return data
# pyxlsb
elif pyxlsb and isinstance(sheet, pyxlsb.worksheet.Worksheet):
errors = {"0x0": "#NULL!", "0x7": "#DIV/0!", "0xf": "#VALUE!",
"0x17": "#REF!", "0x1d": "#NAME?", "0x24": "#NUM!",
"0x2a": "#N/A"}
if not isinstance(first_cell, tuple):
first_cell = xl_cell_to_rowcol(first_cell)
first_cell = (first_cell[0] + 1, first_cell[1] + 1)
if last_cell and not isinstance(last_cell, tuple):
last_cell = xl_cell_to_rowcol(last_cell)
last_cell = (last_cell[0] + 1, last_cell[1] + 1)
data = []
# sheet.rows() is a generator that requires islice to slice it
for row in itertools.islice(sheet.rows(),
first_cell[0] - 1,
last_cell[0] if last_cell else None):
data.append([errors.get(cell.v, cell.v) for cell in row]
[first_cell[1] - 1 : last_cell[1] if last_cell else None])
return data
else:
raise TypeError(f"Couldn't handle sheet of type {type(sheet)}")
def write(sheet, values, first_cell="A1", date_format=None):
"""Write a 2-dimensional list to an Excel range.
Parameters
----------
sheet : object
An openpyxl, xlsxwriter or xlwt sheet object. openpyxl's
write_only=True mode is not supported.
values : list
A 2-dimensional list of values
first_cell : str or tuple, optional
Top-left corner of the Excel range where you want to write out
the DataFrame. Can be a string like "A1" or a row/col tuple
like (1, 1), default is "A1".
date_format : str, optional
Only accepted if sheet is an openpyxl or xlwt sheet. By default,
formats dates in the following format: "mm/dd/yy". For xlsxwriter,
set the format when you instantiate a Workbook by providing:
options={"default_date_format": "mm/dd/yy"}
"""
# OpenPyXL
if openpyxl and isinstance(
sheet, openpyxl.worksheet.worksheet.Worksheet):
if date_format is None:
date_format = "mm/dd/yy"
if not isinstance(first_cell, tuple):
first_cell = openpyxl.utils.coordinate_to_tuple(first_cell)
for i, row in enumerate(values):
for j, value in enumerate(row):
cell = sheet.cell(row=first_cell[0] + i,
column=first_cell[1] + j)
cell.value = value
if date_format and isinstance(value, (dt.datetime, dt.date)):
cell.number_format = date_format
# XlsxWriter
elif xlsxwriter and isinstance(sheet, xlsxwriter.worksheet.Worksheet):
if date_format is not None:
raise ValueError("date_format must be set as Workbook option")
if isinstance(first_cell, tuple):
first_cell = first_cell[0] - 1, first_cell[1] - 1
else:
first_cell = xl_cell_to_rowcol(first_cell)
for r, row_data in enumerate(values):
sheet.write_row(first_cell[0] + r, first_cell[1], row_data)
# xlwt
elif xlwt and isinstance(sheet, xlwt.Worksheet):
if date_format is None:
date_format = "mm/dd/yy"
date_format = xlwt.easyxf(num_format_str=date_format)
if isinstance(first_cell, tuple):
first_cell = (first_cell[0] - 1, first_cell[1] - 1)
else:
first_cell = xl_cell_to_rowcol(first_cell)
for i, row in enumerate(values):
for j, cell in enumerate(row):
if isinstance(cell, (dt.datetime, dt.date)):
sheet.write(i + first_cell[0], j + first_cell[1],
cell, date_format)
else:
sheet.write(i + first_cell[0], j + first_cell[1],
cell)
else:
raise TypeError(f"Couldn't handle sheet of type {type(sheet)}")
def xl_cell_to_rowcol(cell_str):
"""
Convert a cell reference in A1 notation to a zero indexed row and column.
Args:
cell_str: A1 style string.
Returns:
row, col: Zero indexed cell row and column indices.
This function is from XlsxWriter
Copyright (c) 2013-2020, John McNamara <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. 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.
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.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
"""
if not cell_str:
return 0, 0
match = re.compile(r"(\$?)([A-Z]{1,3})(\$?)(\d+)").match(cell_str)
col_str = match.group(2)
row_str = match.group(4)
# Convert base26 column string to number.
expn = 0
col = 0
for char in reversed(col_str):
col += (ord(char) - ord("A") + 1) * (26 ** expn)
expn += 1
# Convert 1-index to zero-index
row = int(row_str) - 1
col -= 1
return row, col