473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

extract text from ods TableCell using odfpy

Hi there,

I'm losing hair trying to figure out how I can actually get the text
out of an existing .ods file. Currently I have:
#!/usr/bin/python
from odf.opendocument import Spreadsheet
from odf.opendocument import load
from odf.table import TableRow,TableCell
from odf import text
doc = load("/tmp/match_data.ods")
d = doc.spreadsheet
rows = d.getElementsByType(TableRow)
for row in rows:
cells = row.getElementsByType(TableCell)
for cell in cells:
print dir(cell.getElementsByType(text.P))

This is a spreadsheet containing 200 rows, each with 4 cells
containing strings. What I'd like to be able to do is something like:
for row in rows:
cells = row.getElementsByType(TableCell)

users.append((cells[0].value,cells[1].value,cells[2].value,cells[3].value))

Thus, what I'd like to know is how to actually get the value out of
the cell. I've read through the odfpy api documentation (which is
almost completely focused on writing, not reading) and googled for
info, but I still haven't found anything.
Aug 25 '08 #1
4 9616
Ok. Sorted it out, but only after taking a round trip over
xml.minidom. Here's the working code:

#!/usr/bin/python
from odf.opendocument import Spreadsheet
from odf.opendocument import load
from odf.table import TableRow,TableCell
from odf.text import P
doc = load("/tmp/match_data.ods")
d = doc.spreadsheet
rows = d.getElementsByType(TableRow)
for row in rows[:2]:
cells = row.getElementsByType(TableCell)
for cell in cells:
tps = cell.getElementsByType(P)
if len(tps) 0:
for x in tps:
print x.firstChild
Aug 26 '08 #2
frankentux wrote:
Ok. Sorted it out, but only after taking a round trip over
xml.minidom. Here's the working code:

#!/usr/bin/python
from odf.opendocument import Spreadsheet
from odf.opendocument import load
from odf.table import TableRow,TableCell
from odf.text import P
doc = load("/tmp/match_data.ods")
d = doc.spreadsheet
rows = d.getElementsByType(TableRow)
for row in rows[:2]:
cells = row.getElementsByType(TableCell)
for cell in cells:
tps = cell.getElementsByType(P)
if len(tps) 0:
for x in tps:
print x.firstChild
--
http://mail.python.org/mailman/listinfo/python-list
=========================
cd /opt
find . -name "*odf*" -print
(empty)
cd /usr/local/lib/python2.5
find . -name "*odf*" -print
(empty)
OK - where is it? :)
Steve
no******@hughes.net
Aug 26 '08 #3
On Aug 27, 3:04 am, norseman <norse...@hughes.netwrote:
frankentux wrote:
Ok. Sorted it out, but only after taking a round trip over
xml.minidom. Here's the working code:
#!/usr/bin/python
from odf.opendocument import Spreadsheet
from odf.opendocument import load
from odf.table import TableRow,TableCell
from odf.text import P
doc = load("/tmp/match_data.ods")
d = doc.spreadsheet
rows = d.getElementsByType(TableRow)
for row in rows[:2]:
cells = row.getElementsByType(TableCell)
for cell in cells:
tps = cell.getElementsByType(P)
if len(tps) 0:
for x in tps:
print x.firstChild
--
http://mail.python.org/mailman/listinfo/python-list

=========================
cd /opt
find . -name "*odf*" -print
(empty)
cd /usr/local/lib/python2.5
find . -name "*odf*" -print
(empty)

OK - where is it? :)
Consider using:
find --http --google "odfpy"
;-)

Aug 26 '08 #4
Ciaran Farrell wrote:
2008/8/26 norseman <no******@hughes.net>:
>frankentux wrote:
>>Ok. Sorted it out, but only after taking a round trip over
xml.minidom. Here's the working code:

#!/usr/bin/python
from odf.opendocument import Spreadsheet
from odf.opendocument import load
from odf.table import TableRow,TableCell
from odf.text import P
doc = load("/tmp/match_data.ods")
d = doc.spreadsheet
rows = d.getElementsByType(TableRow)
for row in rows[:2]:
cells = row.getElementsByType(TableCell)
for cell in cells:
tps = cell.getElementsByType(P)
if len(tps) 0:
for x in tps:
print x.firstChild
--
http://mail.python.org/mailman/listinfo/python-list
=========================
cd /opt
find . -name "*odf*" -print
(empty)
cd /usr/local/lib/python2.5
find . -name "*odf*" -print
(empty)
OK - where is it? :)

Sorry. Stupid of me. The module is not part of the standard libary.
It's at http://opendocumentfellowship.com/projects/odfpy

Ciaran
==============
I got the download and all went pretty well. Setup.py compiled OK and
install put it where it belongs.

As a test I went to try odflint and keep getting a zlib not found error.
It is installed (/usr/local/lib) and the python zlib things .py, .pyc
and .pyo all seem present. Not sure what is happening.
I took a look at Python.2.5.2's zipfile.py

statement: import zlib was changed to import libz as zlib
(ALL libs are prefixed with lib... by convention)
Problem below the test happens with or without my change.

Test I ran:

python
(sign on yah de yah yah)
import zipfile
zipfile.is_zipfile("zx")
False
zipfile.is_zipfile("zz.zip")
True
zipfile.is_zipfile("zx.zip")
False (file non existent - no error generated, but answer correct)

Thus all returned correct answers. Distro Python code runs as expected.

However:

odflint OOstuf2.odt |\__
python /usr/local/bin/odflint OOstuf2.odt |/ Both return following:

Traceback (most recent call last):
File "/usr/local/bin/odflint", line 213, in <module>
lint(sys.argv[1])
File "/usr/local/bin/odflint", line 197, in lint
content = zfd.read(zi.filename)
File "/usr/local/lib/python2.5/zipfile.py", line 498, in read
"De-compression requires the (missing) zlib module"
RuntimeError: De-compression requires the (missing) zlib module

Anybody:
What did I miss correcting? Seems odflint only uses zipfile.references.

System: Slackware 10.2 on 2.4GgHz Laptop
Steve
no******@hughes.net
Aug 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Craig HB | last post by:
I am using an asp.net table to display postings in a classifieds website. This is a section of the HTM I am using... <asp:table id="tblAdvert" Width="466px" Height="78px" runat="server"...
3
by: harry | last post by:
I want to be able to change the text alignment within a table cell between "right" & "center" depending on how many rows are in the table. Is this possible in Javascript? - can't see how to do...
1
by: Bob Voss | last post by:
If I add a hidden input control to a tablecell and then set the cell's text property, the hidden control is not output. If I set the text first, then the hidden control is output but the text is...
2
by: Craig HB | last post by:
I am using an asp.net table to display postings in a classifieds website. This is a section of the HTM I am using... <asp:table id="tblAdvert" Width="466px" Height="78px" runat="server"...
1
by: Krish | last post by:
Hi, I am using table control to create table/cell & 2 text box & one image button during runtime. Onclick event of image button i would like to validate if there is any value in either of text...
16
by: mj.redfox.mj | last post by:
Can anyone help? I have a textbox which I'm programatically adding by using the following code: txtTest = New TextBox txtTest.ID = "txtLeft" + cntCount.ToString...
2
by: JonWB | last post by:
Hi, I am very new to .Net and have recently taken over someone else’s project. I am developing an asp webpage using C#. I have an asp:table that is populated using data from a database....
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
1
by: yogarajan | last post by:
Hi Friend this is my code protected void Page_Load(object sender, EventArgs e) { for (int i = 0; i < 10; i++) { TableRow tr = new TableRow();
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.