RTF / doc / docx text extraction in program writte

2020-07-22 09:08发布

I am writing some program in Qt/C++, and I need to read text from Microsoft Word/RTF/docx files.

And I am looking for some command-line program that can make that extraction. It may be several programs.

The closest thing I found is DocToText, but it has several bugs, so I can't use it. I have also Microsoft Word installed on the PC. Maybe there is some way to read text using it (have no idea how to use COM)?

标签: c++ windows qt
5条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-07-22 09:48

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.

查看更多
3楼-- · 2020-07-22 09:52

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.

查看更多
够拽才男人
4楼-- · 2020-07-22 09:57
爱情/是我丢掉的垃圾
5楼-- · 2020-07-22 10:00

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 ;)

查看更多
戒情不戒烟
6楼-- · 2020-07-22 10:06

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.

查看更多
登录 后发表回答