def render_to_pdf(self, temp_file): tempname = tempfile.mkdtemp() temp_out_file_html = self.generate_temp_file(tempname, suffix='html') temp_out_file_pdf = self.generate_temp_file(tempname, suffix='pdf') ofile = ooxml.read_from_file(temp_file) html = """<html style="height: 100%"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> </head> <body> """ html += unicode(serialize.serialize(ofile.document), 'utf-8') html += "</body></html>" with codecs.open(temp_out_file_html, 'w', 'utf-8') as f: f.write(html) pdfkit.from_file(temp_out_file_html, temp_out_file_pdf) os.remove(temp_out_file_html) return temp_out_file_pdf
def import_file(self, file_path, options={'scale_font_size': True}, **kwargs): # TODO: document this asap self.delegate.notifier = self.notifier self.broken_images = [] self.converted_images = [] book = self.book process_mode = kwargs.get('process_mode', 'overwrite') try: self.dfile = ooxml.read_from_file(file_path) if self.is_chapter_mode: chapter_content = serialize.serialize(self.dfile.document, self._serialize_options) self._import_single_chapter(self.chapter, chapter_content, process_mode) else: chapters = importer.get_chapters( self.dfile.document, options=options, serialize_options=self._serialize_options) self._import_chapters(book, chapters) # save attachments and tyles self._import_attachments(book, self.dfile.document) self._import_styles(book) self.dfile.close() self._check_for_elements() # trigger signal depending on the import mode # TODO: allow attaching user as sender on `book_imported` signal if self.is_chapter_mode: chapter_imported.send(sender=(self.user or self), chapter=self.chapter) else: book_imported.send(sender=self, book=book) except zipfile.BadZipfile: notif_msg = _( "The file could not be imported because it was not saved in the .docx format. Try to open the file in Word and save it as a .docx." ) # noqa self.notifier.error(notif_msg) except Exception as err: err_msg = _( "The docx file you uploaded contains errors and cannot be converted. Please contact customer support." ) # noqa self.notifier.error(err_msg) logger.exception("Error trying to import docx file. Msg: %s" % err)
import sys import logging from lxml import etree import ooxml from ooxml import parse, serialize, importer logging.basicConfig(filename='ooxml.log', level=logging.INFO) if len(sys.argv) > 1: file_name = sys.argv[1] dfile = ooxml.read_from_file(file_name) print("serialize : ", serialize.serialize(dfile.document)) print("serialize style : ", serialize.serialize_styles(dfile.document))
import sys import six import logging import ooxml from ooxml import parse, serialize, importer logging.basicConfig(filename='ooxml.log', level=logging.INFO) if len(sys.argv) > 1: file_name = sys.argv[1] dfile = ooxml.read_from_file(file_name) six.print_("\n-[HTML]-----------------------------\n") six.print_(serialize.serialize(dfile.document)) six.print_("\n-[CSS STYLE]------------------------\n") six.print_(serialize.serialize_styles(dfile.document)) six.print_("\n-[USED STYLES]----------------------\n") six.print_(dfile.document.used_styles) six.print_("\n-[USED FONT SIZES]------------------\n") six.print_(dfile.document.used_font_size)