-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsheet_happens.py
283 lines (229 loc) · 7.97 KB
/
sheet_happens.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python3
"""
Sheet Happens
https://github.com/nuno-andre/sheet-happens
Copyright (C) 2017-2021 Nuno André <[email protected]>
SPDX-License-Identifier: MIT
"""
__version__ = 0, 0, 6
__description__ = ('Simple Excel 2007+ to CSV and JSON converter without '
'dependencies')
from xml.etree.ElementTree import fromstring
from zipfile import ZipFile, BadZipFile
from functools import wraps
from pathlib import Path
import json
import csv
import re
try:
import yaml
__description__ = __description__.replace(
' and JSON', ', JSON, and YAML')
except ImportError:
yaml = None
CELL = re.compile(r'^([A-Z]+)([0-9]+)$').match
MAIN = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'
REL = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'
NS = {'namespaces': {'main': MAIN}}
def lazyproperty(method):
'''Decorator for lazy evaluated properties.
'''
_name = '_' + method.__name__
@wraps(method)
def wrapper(self, *args, **kwargs):
if not hasattr(self, _name):
setattr(self, _name, method(self, *args, **kwargs))
return getattr(self, _name)
return property(wrapper, doc=method.__doc__)
class _colindex(dict):
'''Converts and caches a letter-based col to 0-based coord.
'''
def __missing__(self, col):
v = self[col] = sum(26 * i + ord(x) - 65 for i, x in enumerate(col))
return v
_cols = _colindex()
def _coords(cell):
'''Converts Excel coords to 0-based.
'''
col, row = CELL(cell).groups()
return _cols[col], int(row) - 1
class Book:
'''Excel file (workbook).
Args:
path: Archive's path.
sanitize: Trims strings and replaces line feeds with whitespaces.
'''
def __init__(self, path, sanitize=True):
self.path = Path(path).resolve()
self.sanitize = sanitize
self._names = dict()
def __iter__(self):
return self.sheets.__iter__()
def __next__(self):
return next(self.__iter__())
@lazyproperty
def shared(self):
'''Shared strings
'''
with self.zipfile.open('xl/sharedStrings.xml') as f:
tree = fromstring(f.read())
return [x.text for x in tree.iterfind('.//main:t', **NS)]
@lazyproperty
def sheets(self):
sheets = list()
tag_id = '{{{}}}id'.format(REL)
with ZipFile(str(self.path), 'r') as z:
with z.open('xl/workbook.xml') as f:
tree = fromstring(f.read())
for s in tree.iterfind('.//main:sheet', **NS):
id = s.get(tag_id).replace('rId', '')
self._names[int(id)] = s.get('name')
self.zipfile = z
for path in z.namelist():
if path.startswith('xl/worksheets/sheet'):
with z.open(path) as f:
sheets.append(Sheet(Path(path), f.read(), self))
self.shared
return sheets
class Sheet:
'''Worksheet
'''
def __init__(self, path, text, book):
self.book = book
self.path = path
self.tree = fromstring(text)
self.width, self.height = self.shape()
def __iter__(self):
return self.dict.__iter__()
def __next__(self):
return next(self.__iter__())
@lazyproperty
def name(self):
pad = len(str(list(self.book._names.keys())[-1]))
ix = int(self.path.stem.replace('sheet', ''))
return '{i:0{p}}_{n}'.format(i=ix, p=pad, n=self.book._names[ix])
def shape(self):
'''Returns table dimensions.
'''
dim = self.tree.find('.//main:dimension', **NS)
nw, se = dim.attrib['ref'].split(':')
width, height = (n + 1 for n in _coords(se))
return width, height
def value(self, node):
'''Returns cell's value.
'''
try:
v = node.find('.//main:v', **NS).text
if node.attrib.get('t') == 's':
value = self.book.shared[int(v)]
else:
value = v
except AttributeError:
value = None
if self.book.sanitize and value:
return ' '.join(filter(None, value.strip().splitlines()))
else:
return value
@lazyproperty
def parsed(self):
'''Returns parsed sheet preallocating rows.
'''
parsed = [[None for _ in range(self.width)]
for _ in range(self.height)]
for node in self.tree.findall('.//main:c', **NS):
col, row = _coords(node.attrib['r'])
value = self.value(node)
parsed[row][col] = value
return parsed
def parse(self):
'''Returns a parsed rows generator.
'''
row = [None for _ in range(self.width)]
lastcol = self.width - 1
for node in self.tree.findall('.//main:c', **NS):
col, _ = _coords(node.attrib['r'])
row[col] = self.value(node)
if col == lastcol:
yield row
row = [None for _ in range(self.width)]
def to_dict(self):
'''Returns a list of dicts generator.
'''
output = self.parse()
header = next(output)
return (dict(zip(header, row)) for row in output)
@lazyproperty
def dict(self):
if hasattr(self, '_parsed'):
header, *rows = self.parsed
return [dict(zip(header, row)) for row in rows]
else:
return list(self.to_dict())
def filedes(self, ext, dirpath=None, mode='w', newline='\n'):
'''Returns a file descriptor.
Path defaults to:
``<file_path>/<file_stem>/<sheet_no>_<sheet_name>.<ext>``
'''
if dirpath:
dirpath = Path(dirpath)
else:
# set excel archive's filename as directory
dirpath = self.book.path.parent / self.book.path.stem
try:
dirpath.mkdir(exist_ok=True, parents=True)
except FileExistsError:
err = f"cannot creake directory '{dirpath}'"
raise ValueError(err) from None
path = dirpath / f'{self.name}.{ext}'
return open(str(path), mode, newline=newline)
def to_csv(self, path=None):
with self.filedes('csv', path, 'w', newline='') as f:
writer = csv.writer(f)
for row in self.parse():
row = [c.encode('utf8').decode('utf-8') for c in row]
writer.writerow(row)
return True
def to_json(self, path=None):
with self.filedes('json', path, 'w', newline='') as f:
json.dump(self.dict, f, indent=4, ensure_ascii=False)
return True
def to_yaml(self, path=None):
with self.filedes('yaml', path, 'w', newline='') as f:
f.write(yaml.dump(self.dict, default_flow_style=False,
allow_unicode=True, sort_keys=False))
return True
def main():
import argparse
p = argparse.ArgumentParser(
prog='sheet-happens',
description=__description__,
)
p.add_argument('filepath')
p.add_argument('--csv', action='store_const', const=True)
p.add_argument('--json', action='store_const', const=True)
if yaml:
p.add_argument('--yaml', action='store_const', const=True)
args = vars(p.parse_args())
path = args.pop('filepath')
fmts = [k for k, v in args.items() if v]
if not fmts:
print('\nERROR. Choose at least one output format.\n')
p.print_help()
return 1
try:
for sheet in Book(path):
for fmt in fmts:
print("Saving '{}' as '{}'".format(sheet.name, fmt))
method = 'to_{}'.format(fmt)
getattr(sheet, method)()
return 0
except BadZipFile:
msg = "ERROR. '{}' is not an Excel 2007+ file"
print(msg.format(path))
except Exception as e:
print('ERROR. {!r}'.format(e))
return 1
__all__ = ['Book', 'Sheet']
if __name__ == '__main__':
import sys
sys.exit(main())