python convert microsoft office docs to plain text

2019-01-14 22:59发布

问题:

Any recomendations on a method to convert .doc, .ppt, and .xls to plain text on linux using python? Really any method of conversion would be useful. I have already looked at using Open Office but, I would like a solution that does not require having to install Open Office.

回答1:

I'd go for the command line-solution (and then use the Python subprocess module to run the tools from Python).

Convertors for msword (catdoc), excel (xls2csv) and ppt (catppt) can be found (in source form) here: http://vitus.wagner.pp.ru/software/catdoc/.

Can't really comment on the usefullness of catppt but catdoc and xls2csv work great!

But be sure to first search your distributions repositories... On ubuntu for example catdoc is just one fast apt-get away.



回答2:

You can access OpenOffice via Python API.

Try using this as a base: http://wiki.services.openoffice.org/wiki/Odt2txt.py



回答3:

The usual tool for converting Microsoft Office documents to HTML or other formats was mswordview, which has since been renamed to vwWare.

If you're looking for a command-line tool, they actually recommend using AbiWord to perform the conversion:

AbiWord --to=txt

If you're looking for a library, start on the wvWare overview page. They also maintain a list of libraries and tools which read MS Office documents.



回答4:

For dealing with Excel Spreadsheets xlwt is good. But it won't help with .doc and .ppt files.

(You may have also heard of PyExcelerator. xlwt is a fork of this and better maintained so I think you'd be better of with xlwt.)



回答5:

At the command line, antiword or wv work very nicely for .doc files. (Not a Python solution, but they're easy to install and fast.)



回答6:

Same problem here. Below is my simple script to convert all doc files in dir 'docs/' to dir 'txts/' using catdoc. Hope it will help someone:

#!/usr/bin/env python 
# -*- coding: utf-8 -*-

import glob, re, os
f = glob.glob('docs/*.doc') + glob.glob('docs/*.DOC')

outDir = 'txts'
if not os.path.exists(outDir):
    os.makedirs(outDir)
for i in f:
    os.system("catdoc -w '%s' > '%s'" %
              (i, outDir + '/' + re.sub(r'.*/([^.]+)\.doc', r'\1.txt', i,
                                   flags=re.IGNORECASE)))


回答7:

I've had some success at using XSLT to process the XML-based office files into something usable in the past. It's not necessarily a python-based solution, but it does get the job done.