camelot.camelot.view.export.word

Package camelot :: Package camelot :: Package view :: Package export :: Module word |
[hide private]
[frames] | no frames]

# Source Code for Module camelot.camelot.view.export.word

     1  import logging
     2  logger = logging.getLogger('camelot.view.export.word')
     3

     4 -def open_html_in_word(html):
     5    import tempfile
     6    import os
     7    from camelot.view import art
     8    html_fd, html_fn = tempfile.mkstemp(suffix='.html')
     9    html_file = os.fdopen(html_fd, 'wb')
    10    html_file.write(html.encode('utf-8'))
    11    html_file.close()
    12

    13    try:
    14      import pythoncom
    15      import win32com.client
    16      pythoncom.CoInitialize()
    17      word_app = win32com.client.Dispatch("Word.Application")
    18    except Exception, e:
    19      """We're probably not running windows, so try abiword"""
    20      logger.warn('unable to launch word', exc_info=e)
    21      os.system('abiword "%s"'%html_fn)
    22      return
    23

    24    doc_fd, doc_fn = tempfile.mkstemp(suffix='.doc')
    25    os.close(doc_fd)
    26    word_app.Visible = True
    27    doc = word_app.Documents.Open(art.file_('empty_document.doc'))
    28    word_app.ActiveDocument.SaveAs(doc_fn)
    29    section = doc.Sections(1)
    30    section.Range.InsertFile(FileName=html_fn)
    31