Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ python:

stages:
- lint
- moban
- test


Expand All @@ -23,16 +22,6 @@ stages:
stage: lint
script: make lint

.moban: &moban
python: 3.6
env:
- MINREQ=0
stage: moban
install: pip install moban gitfs2 pypifs moban-jinja2-github moban-ansible
script:
- moban
- git diff --exit-code

jobs:
include:
- *moban
Expand Down
5 changes: 3 additions & 2 deletions pyexcel-ods3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ test_dependencies:
- pyexcel
- psutil
- pyexcel-xls
description: |
A wrapper library to read, manipulate and write data in ods format
moban_command: false
description: |-
A wrapper library to read, manipulate and write data in ods format
30 changes: 11 additions & 19 deletions pyexcel_ods3/odsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,35 @@

import ezodf
import pyexcel_io.service as service
from pyexcel_io.plugin_api.abstract_sheet import ISheet
from pyexcel_io.plugin_api.abstract_reader import IReader
from pyexcel_io.plugin_api import ISheet, IReader, NamedContent


class ODSSheet(ISheet):
"""ODS sheet representation"""

def __init__(self, sheet, auto_detect_int=True, **keywords):
def __init__(self, sheet, auto_detect_int=True):
self.auto_detect_int = auto_detect_int
self._native_sheet = sheet
self._keywords = keywords
self.ods_sheet = sheet

@property
def name(self):
return self._native_sheet.name
return self.ods_sheet.name

def row_iterator(self):
"""
Number of rows in the xls sheet
"""
return range(self._native_sheet.nrows())
return range(self.ods_sheet.nrows())

def column_iterator(self, row):
"""
Number of columns in the xls sheet
"""
for column in range(self._native_sheet.ncols()):
for column in range(self.ods_sheet.ncols()):
yield self.cell_value(row, column)

def cell_value(self, row, column):
cell = self._native_sheet.get_cell((row, column))
cell = self.ods_sheet.get_cell((row, column))
cell_type = cell.value_type
ret = None
if cell_type == "currency":
Expand All @@ -67,19 +65,19 @@ def cell_value(self, row, column):

class ODSBook(IReader):
def __init__(self, file_alike_object, file_type, **keywords):
self._native_book = ezodf.opendoc(file_alike_object)
self.ods_book = ezodf.opendoc(file_alike_object)
self._keywords = keywords
self.content_array = [
NameObject(sheet.name, sheet) for sheet in self._native_book.sheets
NamedContent(sheet.name, sheet) for sheet in self.ods_book.sheets
]

def read_sheet(self, native_sheet_index):
native_sheet = self.content_array[native_sheet_index].sheet
native_sheet = self.content_array[native_sheet_index].payload
sheet = ODSSheet(native_sheet, **self._keywords)
return sheet

def close(self):
self._native_book = None
self.ods_book = None


class ODSBookInContent(ODSBook):
Expand All @@ -90,9 +88,3 @@ class ODSBookInContent(ODSBook):
def __init__(self, file_content, file_type, **keywords):
io = BytesIO(file_content)
super().__init__(io, file_type, **keywords)


class NameObject(object):
def __init__(self, name, sheet):
self.name = name
self.sheet = sheet
25 changes: 12 additions & 13 deletions pyexcel_ods3/odsw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

ods writer using ezodf

:copyright: (c) 2015-2017 by Onni Software Ltd. & its contributors
:copyright: (c) 2015-2020 by Onni Software Ltd. & its contributors
:license: New BSD License
"""
import types
Expand All @@ -13,8 +13,7 @@
import pyexcel_io.service as service
from pyexcel_io.constants import MAX_INTEGER
from pyexcel_io.exceptions import IntegerAccuracyLossError
from pyexcel_io.plugin_api.abstract_sheet import ISheetWriter
from pyexcel_io.plugin_api.abstract_writer import IWriter
from pyexcel_io.plugin_api import IWriter, ISheetWriter


class ODSSheetWriter(ISheetWriter):
Expand All @@ -23,12 +22,12 @@ class ODSSheetWriter(ISheetWriter):
"""

def __init__(self, ods_book, ods_sheet, sheet_name, **keywords):
self._native_book = ods_book
self._native_sheet = ezodf.Sheet(sheet_name)
self.ods_book = ods_book
self.ods_sheet = ezodf.Sheet(sheet_name)
self.current_row = 0

def _set_size(self, size):
self._native_sheet.reset(size=size)
self.ods_sheet.reset(size=size)

def write_row(self, array):
"""
Expand All @@ -48,7 +47,7 @@ def write_row(self, array):
elif value_type == "float":
if cell > MAX_INTEGER:
raise IntegerAccuracyLossError("%s is too big" % cell)
self._native_sheet[self.current_row, count].set_value(
self.ods_sheet[self.current_row, count].set_value(
cell, value_type=value_type
)
count += 1
Expand All @@ -71,7 +70,7 @@ def close(self):
This call writes file

"""
self._native_book.sheets += self._native_sheet
self.ods_book.sheets += self.ods_sheet


class ODSWriter(IWriter):
Expand All @@ -84,23 +83,23 @@ def __init__(
self, file_alike_object, file_type, skip_backup=True, **keywords
):
"""open a file for writing ods"""
self._native_book = ezodf.newdoc(
self.ods_book = ezodf.newdoc(
doctype=file_type, filename=file_alike_object
)

if skip_backup:
self._native_book.backup = False
self.ods_book.backup = False

def create_sheet(self, name):
"""
write a row into the file
"""
return ODSSheetWriter(self._native_book, None, name)
return ODSSheetWriter(self.ods_book, None, name)

def close(self):
"""
This call writes file

"""
self._native_book.save()
self._native_book = None
self.ods_book.save()
self.ods_book = None
18 changes: 10 additions & 8 deletions tests/test_ods_reader.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import os

from base import ODSCellTypes
from pyexcel_ods3.odsr import ODSBook
from pyexcel_io.reader import Reader
from pyexcel_ods3.odsw import ODSWriter


class TestODSReader(ODSCellTypes):
def setUp(self):
r = ODSBook(
os.path.join("tests", "fixtures", "ods_formats.ods"), "ods"
)
r = Reader("ods")
r.open(os.path.join("tests", "fixtures", "ods_formats.ods"))
self.data = r.read_all()
for key in self.data.keys():
self.data[key] = list(self.data[key])
Expand All @@ -18,20 +17,23 @@ def setUp(self):

class TestODSWriter(ODSCellTypes):
def setUp(self):
r = ODSBook(
r = Reader("ods")
r.open(
os.path.join("tests", "fixtures", "ods_formats.ods"),
"ods",
skip_empty_rows=True,
)
self.data1 = r.read_all()
r.close()
self.testfile = "odswriter.ods"
w = ODSWriter(self.testfile, "ods")
w.write(self.data1)
w.close()
r2 = ODSBook(self.testfile, "ods")
self.data = r2.read_all()
r.open(self.testfile)
self.data = r.read_all()

for key in self.data.keys():
self.data[key] = list(self.data[key])
r.close()

def tearDown(self):
if os.path.exists(self.testfile):
Expand Down
6 changes: 2 additions & 4 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from base import PyexcelWriterBase, PyexcelHatWriterBase
from pyexcel_ods3.odsr import ODSBook as Reader
from pyexcel_ods3 import get_data
from pyexcel_ods3.odsw import ODSWriter as Writer


Expand All @@ -16,12 +16,10 @@ def test_write_book(self):
writer = Writer(self.testfile, "ods")
writer.write(self.content)
writer.close()
reader = Reader(self.testfile, "ods")
content = reader.read_all()
content = get_data(self.testfile)
for key in content.keys():
content[key] = list(content[key])
assert content == self.content
reader.close()

def tearDown(self):
if os.path.exists(self.testfile):
Expand Down