tags:

views:

620

answers:

5

I am writing some program in qt/c++, and i need to read text from ms word/rtf/docx files . And i am looking for some cmd program that can make that extraction . It may be several progs. the closest thing i found is http://silvercoders.com/index.php?page=DocToText but it has several bugs so i cant use it . I have also ms word installed on the pc , maybe there some way read text using it (have no idea how to use com)?

Thanks for help

A: 

I recommend not to use COM as this would defeat the usage of a portable library like Qt in the first place.

You might want to use the classic catdoc or a similar tool such as wvWare.

Note that although the catdoc author claims that catdoc doesn't work under Windows, there is a posting of 2001 which states the opposite.

vog
Does catdoc work with docx files though?
Chirael
A: 

Try Apache Tika

Jaime Pardos
A: 

To read .doc files you can use the structured storage API. A .doc is basically a structured storage repository with various streams corresponding to the various parts of the document.
Be warned that it is quite a hairy API and that even using this API, a .doc file can be quite messy to look at.
Ofcouse this is still windows only but atleast it's not COM. just a plain old C API.

shoosh
I am trying to do it platform independent .And i think there is several programs out there that do those things, but needed to be found .thanks anyway
Night Walker
+2  A: 

Now, this is pretty ugly and pretty hacky, but it seems to work for me for basic text extraction. Obviously to use this in a Qt program you'd have to spawn a process for it etc, but the command line I've hacked together is:

unzip -p file.docx | grep '<w:t' | sed 's/<[^<]*>//g' | grep -v '^[[:space:]]*$'

So that's:

unzip -p file.docx: -p == "unzip to stdout"

grep '<w:t': Grab just the lines containing '<w:t' (<w:t> is the Word 2007 XML element for "text", as far as I can tell)

sed 's/<[^<]>//g'*: Remove everything inside tags

grep -v '^[[:space:]]$'*: Remove blank lines

There is likely a more efficient way to do this, but it seems to work for me on the few docs I've tested it with.

As far as I'm aware, unzip, grep and sed all have ports for Windows and any of the Unixes, so it should be reasonably cross-platform. Despit being a bit of an ugly hack ;)

Ben Williams
This would work for docx files... depends on how well you want to know openxml sdk though... if you just want text without it being too complicated... this would work
jle
A: 

This might help. It is cross-platform and has an API http://www.winfield.demon.nl/

Otherwise the iFilter methods are the way to go if this is windows only. It will allow you to parse anything that has an iFilter on your system. Here is examples of this http://the-lazy-programmer.com/blog/?p=8 . I have used iFilter from the C# end of things quite a bit.

Beached
Also, you can try http://wvware.sourceforge.net for wvLib. It is used by abiword
Beached