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?
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.
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