| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import division, print_function, absolute_import |
| 3 |
|
| 4 |
|
| 5 |
import locale |
| 6 |
|
| 7 |
|
| 8 |
from petl.compat import izip_longest, next, xrange, BytesIO |
| 9 |
from petl.util.base import Table |
| 10 |
from petl.io.sources import read_source_from_arg, write_source_from_arg |
| 11 |
|
| 12 |
|
| 13 |
def fromxls(filename, sheet=None, use_view=True, **kwargs): |
| 14 |
""" |
| 15 |
Extract a table from a sheet in an Excel .xls file. |
| 16 |
|
| 17 |
Sheet is identified by its name or index number. |
| 18 |
|
| 19 |
N.B., the sheet name is case sensitive. |
| 20 |
|
| 21 |
""" |
| 22 |
|
| 23 |
return XLSView(filename, sheet=sheet, use_view=use_view, **kwargs) |
| 24 |
|
| 25 |
|
| 26 |
class XLSView(Table): |
| 27 |
|
| 28 |
def __init__(self, filename, sheet=None, use_view=True, **kwargs): |
| 29 |
self.filename = filename |
| 30 |
self.sheet = sheet |
| 31 |
self.use_view = use_view |
| 32 |
self.kwargs = kwargs |
| 33 |
|
| 34 |
def __iter__(self): |
| 35 |
|
| 36 |
# prefer implementation using xlutils.view as dates are automatically |
| 37 |
# converted |
| 38 |
if self.use_view: |
| 39 |
from petl.io import xlutils_view |
| 40 |
source = read_source_from_arg(self.filename) |
| 41 |
with source.open('rb') as source2: |
| 42 |
source3 = source2.read() |
| 43 |
wb = xlutils_view.View(source3, **self.kwargs) |
| 44 |
if self.sheet is None: |
| 45 |
ws = wb[0] |
| 46 |
else: |
| 47 |
ws = wb[self.sheet] |
| 48 |
for row in ws: |
| 49 |
yield tuple(row) |
| 50 |
else: |
| 51 |
import xlrd |
| 52 |
source = read_source_from_arg(self.filename) |
| 53 |
with source.open('rb') as source2: |
| 54 |
source3 = source2.read() |
| 55 |
with xlrd.open_workbook(file_contents=source3, |
| 56 |
on_demand=True, **self.kwargs) as wb: |
| 57 |
if self.sheet is None: |
| 58 |
ws = wb.sheet_by_index(0) |
| 59 |
elif isinstance(self.sheet, int): |
| 60 |
ws = wb.sheet_by_index(self.sheet) |
| 61 |
else: |
| 62 |
ws = wb.sheet_by_name(str(self.sheet)) |
| 63 |
for rownum in xrange(ws.nrows): |
| 64 |
yield tuple(ws.row_values(rownum)) |
| 65 |
|
| 66 |
|
| 67 |
def toxls(tbl, filename, sheet, encoding=None, style_compression=0, |
| 68 |
styles=None): |
| 69 |
""" |
| 70 |
Write a table to a new Excel .xls file. |
| 71 |
|
| 72 |
""" |
| 73 |
|
| 74 |
import xlwt |
| 75 |
if encoding is None: |
| 76 |
encoding = locale.getpreferredencoding() |
| 77 |
wb = xlwt.Workbook(encoding=encoding, style_compression=style_compression) |
| 78 |
ws = wb.add_sheet(sheet) |
| 79 |
|
| 80 |
if styles is None: |
| 81 |
# simple version, don't worry about styles |
| 82 |
for r, row in enumerate(tbl): |
| 83 |
for c, v in enumerate(row): |
| 84 |
ws.write(r, c, label=v) |
| 85 |
else: |
| 86 |
# handle styles |
| 87 |
it = iter(tbl) |
| 88 |
try: |
| 89 |
hdr = next(it) |
| 90 |
flds = list(map(str, hdr)) |
| 91 |
for c, f in enumerate(flds): |
| 92 |
ws.write(0, c, label=f) |
| 93 |
if f not in styles or styles[f] is None: |
| 94 |
styles[f] = xlwt.Style.default_style |
| 95 |
except StopIteration: |
| 96 |
pass # no header written |
| 97 |
# convert to list for easy zipping |
| 98 |
styles = [styles[f] for f in flds] |
| 99 |
for r, row in enumerate(it): |
| 100 |
for c, (v, style) in enumerate(izip_longest(row, styles, |
| 101 |
fillvalue=None)): |
| 102 |
ws.write(r+1, c, label=v, style=style) |
| 103 |
|
| 104 |
target = write_source_from_arg(filename) |
| 105 |
with target.open('wb') as target2: |
| 106 |
wb.save(target2) |
| 107 |
|
| 108 |
|
| 109 |
Table.toxls = toxls |