How can I count words in complex documents (.rtf, .doc, .odt, etc)?

青春壹個敷衍的年華 提交于 2019-12-06 04:23:24

问题


I'm trying to write a Python function that, given the path to a document file, returns the number of words in that document. This is fairly easy to do with .txt files, and there are tools that allow me to hack support for a few more complex document formats together, but I want a really comprehensive solution.

Looking at OpenOffice.org's py-uno scripting interface and list of supported formats, it would seem ideal to load the documents in a headless OOo and call its word-count function. However, I can't find any py-uno tutorials or sample code that go beyond basic document generation, and even the code snippets I have found are out of date by a half-decade and no longer work.

Whether by using OOo and Uno or not, how can I get reliable word-counts for documents of various formats?


回答1:


load the documents in a headless OOo and call its word-count function

PyODConverter is a recent (11-2009) script to use OOo to convert multiple file types. Looking at the script, it has basic loading of all the OOo supported documents.

This is how you start OOo as a headless service:

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

Then you just have to write a small bootstrapper that calls OOo on the commandline, runs your script, then closes OOo.





回答2:


This could be not the option for you, but in case it is - you can upload documents to Google Docs and then export in .txt format. Google usually does very nice job for the conversion.

You can find relevant APIs here: http://code.google.com/intl/pl/apis/documents/docs/1.0/developers_guide_python.html

Take a look at login, uploading and exporting sections.



来源:https://stackoverflow.com/questions/2256881/how-can-i-count-words-in-complex-documents-rtf-doc-odt-etc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!