Coder Social home page Coder Social logo

read-ods-with-odfpy's Introduction

read-ods-with-odfpy

As seen on: http://www.marco83.com/work/173/read-an-ods-file-with-python-and-odfpy

Odfpy is a python library to read and write OpenDocument documents (such as the .odt or.ods created with LibreOffice or OpenOffice). However, the documentation and examples shipped with Odfpy are more oriented to writing new documents rather than reading existing ones.

Failing to find any simple spreadsheet reading code snippet on the internet, I wrote a simple ODS reader in python that reads an entire .ods file in a dictionary of sheets, where each sheets is stored as an array of arrays (rows, columns). It still requires Odfpy to run.

It has been tested with odfpy 0.9.3 and 1.3.0, python 2.7 and 3.4, using ods files created with OpenOffice.org.

Usage example:

from ODSReader import ODSReader

doc = ODSReader(u'films.ods', clonespannedcolumns=True)
table = doc.getSheet(u'Sheet1')
for i in range(len(table)):
    for j in range(len(table[i])):
        print (table[i][j])

Requirements

  • odfpy 0.9.3 or 1.3.0
  • python 2.7 or 3.4

read-ods-with-odfpy's People

Contributors

embee avatar interestsfantastic avatar marcoconti83 avatar vazhnov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

read-ods-with-odfpy's Issues

Wrong object used in line 72

Lines 71/72 currently read:

if (c.nodeType == 3):
     textContent = u'{}{}'.format(textContent, n.data)

However, I think they should read:

if (c.nodeType == 3):
    textContent = u'{}{}'.format(textContent, c.data)

Otherwise, ODSReader crashes immediately while trying to parse an ODS file with the Exception:

File "xxx/ODSReader.py", line 72, in readSheet
    textContent = u'{}{}'.format(textContent, n.data)

The parser works with the fix suggested above.

What about reading numbers?

Your example works great for reading text, but there seems to be no example for reading numbers. It would be nice if there was!

Rename odf-to-array.py to ODSReader.py

In examples, code

from ODSReader import ODSReader

not work before renaming odf-to-array.py to ODSReader.py.
I think it is need to rename file in git repository or change examples.

spreadsheet is a text object?

I've been struggling with odfpy and every library that depends on it for a few hours.

line 35:
for sheet in self.doc.spreadsheet.getElementsByType(Table):
gives an error:
AttributeError: 'Text' object has no attribute 'getElementsByType'

remove 'spreadsheet' seems to fix the problem.
new line 35:
for sheet in self.doc.getElementsByType(Table):

I don't know if this is just fixing the symptom but has solved all my current problems.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.