Python, web2py – open MS Word file on-the-fly

Recently I was seeking how to open MS Word file on-the-fly for processing by the python-docx library. By trials and errors I could get the code work. I use web2py framework as a wrapper of POST request.

import docx
from StringIO import StringIO         
source_stream = StringIO(request.vars['file'].value)
document = docx.Document(source_stream)
source_stream.close()

request here (request.vars[‘file’].value) is web2py framework own core class (global object), containing a full GET or POST request information.

A full controller form procedure:

import docx
from StringIO import StringIO
def form():    
    form=FORM(TABLE(TR("",INPUT(_type="file",_name="file" ), 
                    'France or UK', SELECT('fr', 'uk',_name="fr_uk")),
                    TR("",INPUT(_type="submit",_value="Submit"))))
    if form.accepts(request):
        try:
            source_stream = StringIO(request.vars['file'].value)
            document = docx.Document(source_stream)
            source_stream.close()
            if request.vars['fr_uk'] == 'fr':
                FR_UK = 0
            else:
                FR_UK = 1
            document = process_doc(document, FR_UK)
            new_filename = request.vars['file'].filename.replace('.docx', '-modified.docx')
            filepath = os.path.join(request.folder, 'uploads', new_filename )
            document.save( filepath )
        except Exception as e:
            return dict(form=form , error = e)
            response.flash = "MS Word file {0} opening exception:".format(request.vars['file'].filename), e

    elif form.errors:
        response.flash="File is invalid"
    else:
        response.flash="Please fill the form out correctly."

    return dict(form=form, fvars=form.vars.myfile )

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.