reverted to flask based extension management. pkgutil and namespace packages do not fit well with the development though their import statement looks attractive

This commit is contained in:
chfw 2014-12-05 22:48:41 +00:00
parent 27fbfac260
commit fecf468a7e
8 changed files with 35 additions and 217 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.pyc
*~
.coverage

View File

@ -12,7 +12,7 @@ python:
install:
- python setup.py install
- pip install git+https://github.com/chfw/pyexcel.git
- pip install git+https://github.com/chfw/pyexcel.ioext.git
- pip install git+https://github.com/chfw/pyexcel_io.git
- pip install -r tests/requirements.txt
script:
make test

View File

@ -1,36 +1,36 @@
===========
pyexcel-xl
pyexcel-xls
===========
.. image:: https://api.travis-ci.org/chfw/pyexcel-xl.png
:target: http://travis-ci.org/chfw/pyexcel-xl
.. image:: https://api.travis-ci.org/chfw/pyexcel-xls.png
:target: http://travis-ci.org/chfw/pyexcel-xls
.. image:: https://coveralls.io/repos/chfw/pyexcel-xl/badge.png?branch=master
:target: https://coveralls.io/r/chfw/pyexcel-xl?branch=master
.. image:: https://coveralls.io/repos/chfw/pyexcel-xls/badge.png?branch=master
:target: https://coveralls.io/r/chfw/pyexcel-xls?branch=master
.. image:: https://pypip.in/d/pyexcel-xl/badge.png
:target: https://pypi.python.org/pypi/pyexcel-xl
.. image:: https://pypip.in/d/pyexcel-xls/badge.png
:target: https://pypi.python.org/pypi/pyexcel-xls
.. image:: https://pypip.in/py_versions/pyexcel-xl/badge.png
:target: https://pypi.python.org/pypi/pyexcel-xl
.. image:: https://pypip.in/py_versions/pyexcel-xls/badge.png
:target: https://pypi.python.org/pypi/pyexcel-xls
.. image:: https://pypip.in/implementation/pyexcel-xl/badge.png
:target: https://pypi.python.org/pypi/pyexcel-xl
.. image:: https://pypip.in/implementation/pyexcel-xls/badge.png
:target: https://pypi.python.org/pypi/pyexcel-xls
**pyexcel-xl** is a tiny wrapper library to read, manipulate and write data in xls format and it can read xlsx and xlsm fromat. You are likely to use it with `pyexcel <https://github.com/chfw/pyexcel>`_.
**pyexcel-xls** is a tiny wrapper library to read, manipulate and write data in xls format and it can read xlsx and xlsm fromat. You are likely to use it with `pyexcel <https://github.com/chfw/pyexcel>`_.
Installation
============
You can install it via pip::
$ pip install pyexcel-xl
$ pip install pyexcel-xls
or clone it and install it::
$ git clone http://github.com/chfw/pyexcel-xl.git
$ cd pyexcel-xl
$ git clone http://github.com/chfw/pyexcel-xls.git
$ cd pyexcel-xls
$ python setup.py install
Usage
@ -39,7 +39,7 @@ Usage
As a standalone library
------------------------
Write to an xl file
Write to an xls file
*********************
.. testcode::
@ -50,12 +50,12 @@ Write to an xl file
... from StringIO import StringIO
... else:
... from io import BytesIO as StringIO
>>> from pyexcel.ext.xl import OrderedDict
>>> from pyexcel.ext.xls import OrderedDict
Here's the sample code to write a dictionary to an xl file::
>>> from pyexcel.ext.xl import XLWriter
>>> from pyexcel_xls import XLWriter
>>> data = OrderedDict() # from collections import OrderedDict
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})
@ -63,12 +63,12 @@ Here's the sample code to write a dictionary to an xl file::
>>> writer.write(data)
>>> writer.close()
Read from an xl file
Read from an xls file
**********************
Here's the sample code::
>>> from pyexcel.ext.xl import XLBook
>>> from pyexcel_xls import XLBook
>>> book = XLBook("your_file.xls")
>>> # book.sheets() returns a dictionary of all sheet content
@ -78,12 +78,12 @@ Here's the sample code::
>>> print(json.dumps(book.sheets()))
{"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "Sheet 2": [["row 1", "row 2", "row 3"]]}
Write an xl to memory
Write an xls to memory
**********************
Here's the sample code to write a dictionary to an xl file::
>>> from pyexcel.ext.xl import XLWriter
>>> from pyexcel_xls import XLWriter
>>> data = OrderedDict()
>>> data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
>>> data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})
@ -96,7 +96,7 @@ Here's the sample code to write a dictionary to an xl file::
>>> # object for downloading
Read from an xl from memory
Read from an xls from memory
*****************************
Continue from previous example::
@ -114,7 +114,7 @@ As a pyexcel plugin
Import it in your file to enable this plugin::
from pyexcel.ext import xl
from pyexcel.ext import xls
Please note only pyexcel version 0.0.4+ support this.
@ -124,7 +124,7 @@ Reading from an xl file
Here is the sample code::
>>> import pyexcel as pe
>>> from pyexcel.ext import xl
>>> from pyexcel.ext import xls
# "example.xls"
>>> sheet = pe.load_book("your_file.xls")

View File

@ -1,2 +0,0 @@
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)

View File

@ -1,12 +0,0 @@
from .odsbook import ODSBook, ODSWriter
try:
from pyexcel.io import READERS
from pyexcel.io import WRITERS
READERS["ods"] = ODSBook
WRITERS["ods"] = ODSWriter
except:
# to allow this module to function independently
pass
__VERSION__ = "0.0.2"

View File

@ -1,172 +0,0 @@
"""
pyexcel.io.xlbook
~~~~~~~~~~~~~~~~~~~
The lower level xls/xlsx/xlsm file format handler using xlrd/xlwt
:copyright: (c) 2014 by C. W.
:license: GPL v3
"""
import datetime
import xlrd
from xlwt import Workbook, XFStyle
from .._compact import OrderedDict
XLS_FORMAT_CONVERSION = {
xlrd.XL_CELL_TEXT: str,
xlrd.XL_CELL_EMPTY: None,
xlrd.XL_CELL_DATE: datetime.datetime,
xlrd.XL_CELL_NUMBER: float,
xlrd.XL_CELL_BOOLEAN: int,
xlrd.XL_CELL_BLANK: None,
xlrd.XL_CELL_ERROR: None
}
def xldate_to_python_date(value):
"""
convert xl date to python date
"""
date_tuple = xlrd.xldate_as_tuple(value, 0)
ret = None
if date_tuple == (0, 0, 0, 0, 0, 0):
ret = datetime.datetime(1900, 1, 1, 0, 0, 0)
elif date_tuple[0:3] == (0, 0, 0):
ret = datetime.time(date_tuple[3],
date_tuple[4],
date_tuple[5])
elif date_tuple[3:6] == (0, 0, 0):
ret = datetime.date(date_tuple[0],
date_tuple[1],
date_tuple[2])
return ret
class XLSheet:
"""
xls sheet
Currently only support first sheet in the file
"""
def __init__(self, sheet):
self.worksheet = sheet
def number_of_rows(self):
"""
Number of rows in the xls sheet
"""
return self.worksheet.nrows
def number_of_columns(self):
"""
Number of columns in the xls sheet
"""
return self.worksheet.ncols
def cell_value(self, row, column):
"""
Random access to the xls cells
"""
cell_type = self.worksheet.cell_type(row, column)
my_type = XLS_FORMAT_CONVERSION[cell_type]
value = self.worksheet.cell_value(row, column)
if my_type == datetime.datetime:
value = xldate_to_python_date(value)
return value
def to_array(sheet):
array = []
for r in range(0, sheet.number_of_rows()):
row = []
for c in range(0, sheet.number_of_columns()):
row.append(sheet.cell_value(r, c))
array.append(row)
return array
class XLBook:
"""
XLSBook reader
It reads xls, xlsm, xlsx work book
"""
def __init__(self, filename, file_content=None, **keywords):
self.workbook = xlrd.open_workbook(filename, file_contents=file_content)
self.mysheets = OrderedDict()
for name in self.workbook.sheet_names():
data = to_array(XLSheet(
self.workbook.sheet_by_name(name)))
self.mysheets[name] = data
def sheets(self):
"""Get sheets in a dictionary"""
return self.mysheets
class XLSheetWriter:
"""
xls, xlsx and xlsm sheet writer
"""
def __init__(self, wb, name):
self.wb = wb
if name:
sheet_name = name
else:
sheet_name = "pyexcel_sheet1"
self.ws = self.wb.add_sheet(sheet_name)
self.current_row = 0
def set_size(self, size):
pass
def write_row(self, array):
"""
write a row into the file
"""
for i in range(0, len(array)):
value = array[i]
style = None
tmp_array = []
if isinstance(value, datetime.date) or isinstance(value, datetime.datetime):
tmp_array = [value.year, value.month, value.day]
value = xlrd.xldate.xldate_from_date_tuple(tmp_array, 0)
style = XFStyle()
style.num_format_str = "DD/MM/YY"
elif isinstance(value, datetime.time):
tmp_array = [value.hour, value.minute, value.second]
value = xlrd.xldate.xldate_from_time_tuple(tmp_array)
style = XFStyle()
style.num_format_str = "HH:MM:SS"
if style:
self.ws.write(self.current_row, i, value, style)
else:
self.ws.write(self.current_row, i, value)
self.current_row += 1
def close(self):
"""
This call actually save the file
"""
pass
class XLWriter:
"""
xls, xlsx and xlsm writer
"""
def __init__(self, file):
self.file = file
self.wb = Workbook()
self.current_row = 0
def create_sheet(self, name):
return XLSheetWriter(self.wb, name)
def close(self):
"""
This call actually save the file
"""
self.wb.save(self.file)

View File

@ -11,7 +11,7 @@ import sys
import datetime
import xlrd
from xlwt import Workbook, XFStyle
from pyexcel.ioext import SheetReader, BookReader, SheetWriter, BookWriter
from pyexcel_io import SheetReader, BookReader, SheetWriter, BookWriter
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
from ordereddict import OrderedDict
else:

View File

@ -11,18 +11,19 @@ with open("README.rst", 'r') as readme:
dependencies = [
'xlrd',
'xlwt-future'
'xlwt-future',
'pyexcel-io'
]
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
dependencies.append('ordereddict')
setup(
name='pyexcel-xl',
name='pyexcel-xls',
author="C. W.",
version='0.0.1',
author_email="wangc_2011@hotmail.com",
url="https://github.com/chfw/pyexcel-xl",
description='A wrapper library to read, manipulate and write data in xls, xlsx and xlsm format',
url="https://github.com/chfw/pyexcel-xls",
description='A wrapper library to read, manipulate and write data in xls format. It reads xlsx and xlsm format',
install_requires=dependencies,
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
include_package_data=True,