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

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

     1  import logging
     2  from camelot.view import art
     3

     4  logger = logging.getLogger('camelot.view.export.word')
     5

     6 -def open_document_in_word(filename):
     7      """Try to open a document using word and return the word application com object

     8      if succeeded

     9      :return: (word, doc) a tuple of the com objects pointing to the word application, and

    10      the opened document. returns (None, None) if unable to open document using word

    11      """
    12      import sys
    13      if 'win' in sys.platform:
    14          import pythoncom
    15          import win32com.client
    16          pythoncom.CoInitialize()
    17          try:
    18              word_app = win32com.client.Dispatch("Word.Application")
    19          except Exception, e:
    20              logger.info('Unable to open word', exc_info=e)
    21              return (None, None)
    22          word_app.Visible = True
    23          doc = word_app.Documents.Open(filename)
    24          doc.Activate()
    25          word_app.Activate()
    26          return word_app, doc
    27      else:
    28          """We're probably not running windows, so let OS handle it (used to be abiword)"""
    29          from PyQt4 import QtGui, QtCore
    30          QtGui.QDesktopServices.openUrl(QtCore.QUrl('file://%s' % filename))
    31      return (None, None)
    32

    33 -def open_html_in_word(html, template=art.file_('empty_document.doc'),

    34                        pre_processor=lambda doc:None,

    35                        post_processor=lambda doc:None):
    36      """Open MS Word through COM objects and import the specified html

    37      into a new document.

    38      @param html: the html to import

    39      @param template: the empty word document in which to import the html

    40      @param post_processor: a function that will be called before importing the

    41      html, with as its argument the COM Document.

    42      @param post_processor: a function that will be called after importing the

    43      html, with as its argument the COM Document.

    44      """
    45      import tempfile
    46      import os, sys
    47

    48      html_fd, html_fn = tempfile.mkstemp(suffix='.html')
    49      html_file = os.fdopen(html_fd, 'wb')
    50      html_file.write(html.encode('utf-8'))
    51      html_file.close()
    52

    53      word_app = None
    54      if 'win' in sys.platform:
    55          word_app, doc = open_document_in_word(template)
    56

    57      if word_app:
    58          doc_fd, doc_fn = tempfile.mkstemp(suffix='.doc')
    59          os.close(doc_fd)
    60          word_app.ActiveDocument.SaveAs(doc_fn)
    61          section = doc.Sections(1)
    62          pre_processor(doc)
    63          section.Range.InsertFile(FileName=html_fn)
    64          post_processor(doc)
    65      else:
    66  #        self.view = QtWebKit.QWebView(TOP_LEVEL)

    67  #        self.view.load(self.app_admin.get_help_url())

    68  #        self.view.setWindowTitle(_('Help Browser'))

    69  #        self.view.setWindowIcon(self.helpAct.icon())

    70  #        self.view.show()

    71          """We're probably not running windows, so let OS handle it (used to be abiword)"""
    72          from PyQt4 import QtGui, QtCore
    73          if not html_fn.startswith(r'\\'):
    74              url = QtCore.QUrl.fromLocalFile(html_fn)
    75          else:
    76              url = QtCore.QUrl(html_fn, QtCore.QUrl.TolerantMode)
    77          QtGui.QDesktopServices.openUrl(url)
    78          return
    79