Skip to content

Commit

Permalink
Merge pull request #5 from mmulqueen/jopendocument_fix
Browse files Browse the repository at this point in the history
jOpenDocument fix
  • Loading branch information
mmulqueen committed Aug 23, 2015
2 parents b52b2a8 + bb2cd67 commit b2b60e8
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 15 deletions.
26 changes: 21 additions & 5 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import odswriter as ods
with ods.writer(open("test.ods","wb")) as odsfile:
odsfile.writerow(["String", "ABCDEF123456", "123456"])
# Lose the 2L below if you want to run this example code on Python 3, Python 3 has no long type.
odsfile.writerow(["Float", 1, 123, 123.123, 2L, decimal.Decimal("10.321")])
odsfile.writerow(["Date/DateTime", datetime.datetime.now(), datetime.date(1989,11,9)])
odsfile.writerow(["Time",datetime.time(13,37),datetime.time(16,17,18)])
odsfile.writerow(["Bool",True,False,True])
odsfile.writerow(["Formula",1,2,3,ods.Formula("IF(A1=2,B1,C1)")])
odsfile.writerow(["Float", 1, 123, 123.123, decimal.Decimal("10.321")])
odsfile.writerow(["Date/DateTime", datetime.datetime.now(), datetime.date(1989, 11, 9)])
odsfile.writerow(["Time",datetime.time(13, 37),datetime.time(16, 17, 18)])
odsfile.writerow(["Bool", True, False, True])
odsfile.writerow(["Formula", 1, 2, 3, ods.Formula("IF(A1=2,B1,C1)")])

# Multiple sheet mode
with ods.writer(open("test-multi.ods","wb")) as odsfile:
Expand All @@ -45,5 +45,21 @@ with ods.writer(open("test-multi.ods","wb")) as odsfile:
sloths.writerow(["Pygmy Three-Toed Sloth", "Maned Sloth", "Pale-Throated Sloth", "Brown-Throated Sloth",
"Linneaeus's Two-Twoed Sloth", "Hoffman's Two-Toed Sloth"])

Compatibility
-------------
Odswriter is tested (see Travis CI) for compatibility with LibreOffice and Gnumeric. Compatibility with OpenOffice
should be good.

jOpenDocument is not compatible out-of-the-box, but by specifying the number of columns (odswriter will pad with empty
cells up to that number) it can be made compatible. Code example:

```python
import odswriter as ods

with ods.writer(open("test-multi.ods","wb")) as odsfile:
my_sheet = odsfile.new_sheet("My Sheet", cols=3)
my_sheet.writerows([["One"],
["Two", "Four", "Sixteen"],
["Three", "Nine", "Twenty seven"]])

```
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,37 @@ import odswriter as ods
with ods.writer(open("test.ods","wb")) as odsfile:
odsfile.writerow(["String", "ABCDEF123456", "123456"])
# Lose the 2L below if you want to run this example code on Python 3, Python 3 has no long type.
odsfile.writerow(["Float", 1, 123, 123.123, 2L, decimal.Decimal("10.321")])
odsfile.writerow(["Date/DateTime", datetime.datetime.now(), datetime.date(1989,11,9)])
odsfile.writerow(["Time",datetime.time(13,37),datetime.time(16,17,18)])
odsfile.writerow(["Bool",True,False,True])
odsfile.writerow(["Formula",1,2,3,ods.Formula("IF(A1=2,B1,C1)")])
odsfile.writerow(["Float", 1, 123, 123.123, decimal.Decimal("10.321")])
odsfile.writerow(["Date/DateTime", datetime.datetime.now(), datetime.date(1989, 11, 9)])
odsfile.writerow(["Time",datetime.time(13, 37),datetime.time(16, 17, 18)])
odsfile.writerow(["Bool", True, False, True])
odsfile.writerow(["Formula", 1, 2, 3, ods.Formula("IF(A1=2,B1,C1)")])

# Multiple sheet mode
with ods.writer(open("test-multi.ods","wb")) as odsfile:
bears = odsfile.new_sheet("Bears")
bears.writerow(["American Black Bear", "Asiatic Black Bear", "Brown Bear", "Giant Panda", "Qinling Panda",
bears.writerow(["American Black Bear", "Asiatic Black Bear", "Brown Bear", "Giant Panda", "Qinling Panda",
"Sloth Bear", "Sun Bear", "Polar Bear", "Spectacled Bear"])
sloths = odsfile.new_sheet("Sloths")
sloths.writerow(["Pygmy Three-Toed Sloth", "Maned Sloth", "Pale-Throated Sloth", "Brown-Throated Sloth",
"Linneaeus's Two-Twoed Sloth", "Hoffman's Two-Toed Sloth"])
```

Compatibility
-------------
Odswriter is tested (see Travis CI) for compatibility with LibreOffice and Gnumeric. Compatibility with OpenOffice
should be good.

jOpenDocument is not compatible out-of-the-box, but by specifying the number of columns (odswriter will pad with empty
cells up to that number) it can be made compatible. Code example:

```python
import odswriter as ods

with ods.writer(open("test-multi.ods","wb")) as odsfile:
my_sheet = odsfile.new_sheet("My Sheet", cols=3)
my_sheet.writerows([["One"],
["Two", "Four", "Sixteen"],
["Three", "Nine", "Twenty seven"]])

```
26 changes: 23 additions & 3 deletions odswriter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, odsfile):
self.zipf.writestr("styles.xml",
ods_components.styles_xml.encode("utf-8"))
self.default_sheet = None
self.sheets = []

def __enter__(self):
return self
Expand Down Expand Up @@ -73,26 +74,45 @@ def writerows(self, rows):
for row in rows:
self.writerow(row)

def new_sheet(self, name=None):
def new_sheet(self, name=None, cols=None):
"""
Create a new sheet in the spreadsheet and return it so content can be added.
:param name: Optional name for the sheet.
:param cols: Specify the number of columns, needed for compatibility in some cases
:return: Sheet object
"""
return Sheet(self.dom, name)
sheet = Sheet(self.dom, name, cols)
self.sheets.append(sheet)
return sheet


class Sheet(object):
def __init__(self, dom, name="Sheet 1"):
def __init__(self, dom, name="Sheet 1", cols=None):
self.dom = dom
self.cols = cols
spreadsheet = self.dom.getElementsByTagName("office:spreadsheet")[0]
self.table = self.dom.createElement("table:table")
if name:
self.table.setAttribute("table:name", name)
self.table.setAttribute("table:style-name", "ta1")

if self.cols is not None:
col = self.dom.createElement("table:table-column")
col.setAttribute("table:number-columns-repeated", unicode(self.cols))
self.table.appendChild(col)

spreadsheet.appendChild(self.table)

def writerow(self, cells):
row = self.dom.createElement("table:table-row")
content_cells = len(cells)

if self.cols is not None:
padding_cells = self.cols - content_cells
if content_cells > self.cols:
raise Exception("More cells than cols.")
cells += [None]*padding_cells

for cell_data in cells:
cell = self.dom.createElement("table:table-cell")
text = None
Expand Down
1 change: 1 addition & 0 deletions odswriter/ods_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<office:document-styles
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
office:version="1.2">
<office:styles>
<number:time-style style:name="odswriterStyleXMLTime"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from distutils.core import setup

setup(name="odswriter",
version="0.3.0",
version="0.4.0",
description="A pure-Python module for writing OpenDocument spreadsheets (similar to csv.writer).",
author="Michael Mulqueen",
author_email="[email protected]",
Expand Down
63 changes: 63 additions & 0 deletions tests/test_code_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# The MIT License (MIT)
#
# Copyright (c) 2014 - 2015 Michael Mulqueen (http://michael.mulqueen.me.uk/)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from unittest import TestCase

import io
import tempfile
import decimal
import datetime

import odswriter as ods


class TestCodeExamples(TestCase):
def test_single_sheet(self):
f = io.BytesIO()
with ods.writer(f) as odsfile:
odsfile.writerow(["String", "ABCDEF123456", "123456"])
# Lose the 2L below if you want to run this example code on Python 3, Python 3 has no long type.
odsfile.writerow(["Float", 1, 123, 123.123, decimal.Decimal("10.321")])
odsfile.writerow(["Date/DateTime", datetime.datetime.now(), datetime.date(1989, 11, 9)])
odsfile.writerow(["Time", datetime.time(13, 37), datetime.time(16, 17, 18)])
odsfile.writerow(["Bool", True, False, True])
odsfile.writerow(["Formula", 1, 2, 3, ods.Formula("IF(A1=2,B1,C1)")])
val = f.getvalue()
self.assertGreater(len(val), 0)

def test_multi_sheet(self):
f = io.BytesIO()
with ods.writer(f) as odsfile:
bears = odsfile.new_sheet("Bears")
bears.writerow(["American Black Bear", "Asiatic Black Bear", "Brown Bear", "Giant Panda", "Qinling Panda",
"Sloth Bear", "Sun Bear", "Polar Bear", "Spectacled Bear"])
sloths = odsfile.new_sheet("Sloths")
sloths.writerow(["Pygmy Three-Toed Sloth", "Maned Sloth", "Pale-Throated Sloth", "Brown-Throated Sloth",
"Linneaeus's Two-Twoed Sloth", "Hoffman's Two-Toed Sloth"])

def test_col_padding(self):
f = io.BytesIO()
with ods.writer(f) as odsfile:
my_sheet = odsfile.new_sheet("My Sheet", cols=3)
my_sheet.writerows([["One"],
["Two", "Four", "Sixteen"],
["Three", "Nine", "Twenty seven"]])

0 comments on commit b2b60e8

Please sign in to comment.