diff --git a/README b/README index 1f16359..98a537e 100644 --- a/README +++ b/README @@ -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: @@ -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"]]) ``` diff --git a/README.md b/README.md index 8a40c45..1923a61 100644 --- a/README.md +++ b/README.md @@ -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"]]) ``` diff --git a/odswriter/__init__.py b/odswriter/__init__.py index 6f8186f..528b488 100644 --- a/odswriter/__init__.py +++ b/odswriter/__init__.py @@ -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 @@ -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 diff --git a/odswriter/ods_components.py b/odswriter/ods_components.py index caffdb5..ccb3a57 100644 --- a/odswriter/ods_components.py +++ b/odswriter/ods_components.py @@ -59,6 +59,7 @@