def render(self, context, delete_on_close=True): """ Return rendered ODF (webodt.ODFDocument instance)""" # create temp output directory tmpdir = tempfile.mkdtemp() self.handler.unpack(tmpdir) # store updated content.xml for f_to_process in self.get_files_to_process(): template = self.get_file(f_to_process) for preprocess_func in list_preprocessors(self.preprocessors): template = preprocess_func(template) template = Template(template) xml_result = template.render(context) filename = os.path.join(tmpdir, f_to_process) result_fd = open(filename, 'w') result_fd.write(smart_str(xml_result)) result_fd.close() lowlevel_fd, tmpfile = tempfile.mkstemp(suffix='.odt', dir=WEBODT_TMP_DIR) os.close(lowlevel_fd) tmpzipfile = zipfile.ZipFile(tmpfile, 'w') for root, _, files in os.walk(tmpdir): for fn in files: path = os.path.join(root, fn) os.utime(path, (self._fake_timestamp, self._fake_timestamp)) fn = os.path.relpath(path, tmpdir) tmpzipfile.write(path, fn) tmpzipfile.close() # remove directory tree shutil.rmtree(tmpdir) # return ODF document return ODFDocument(tmpfile, delete_on_close=delete_on_close)
def render(self, context, delete_on_close=True): """ Return rendered ODF (webodt.ODFDocument instance)""" # create temp output directory tmpdir = tempfile.mkdtemp() self.handler.unpack(tmpdir) # store updated content.xml template_content = self.get_content_xml() for preprocess_func in list_preprocessors(self.preprocessors): template_content = preprocess_func(template_content) template = Template(template_content) content_xml = template.render(context) content_filename = os.path.join(tmpdir, 'content.xml') content_fd = open(content_filename, 'w', encoding='utf-8') content_fd.write(linebreaksodt(content_xml.strip())) content_fd.close() # create .odt file _, tmpfile = tempfile.mkstemp(suffix='.odt', dir=WEBODT_TMP_DIR) tmpzipfile = zipfile.ZipFile(tmpfile, 'w') for root, _, files in os.walk(tmpdir): for fn in files: path = os.path.join(root, fn) os.utime(path, (self._fake_timestamp, self._fake_timestamp)) fn = os.path.relpath(path, tmpdir) tmpzipfile.write(path, fn) tmpzipfile.close() # remove directory tree shutil.rmtree(tmpdir) # return ODF document return ODFDocument(tmpfile, delete_on_close=delete_on_close)
def render(self, context, delete_on_close=True): """ Return rendered ODF (webodt.ODFDocument instance)""" # create temp output directory tmpdir = tempfile.mkdtemp() self.handler.unpack(tmpdir) # store updated content.xml for f_to_process in self.get_files_to_process(): template = self.get_file(f_to_process) for preprocess_func in list_preprocessors(self.preprocessors): template = preprocess_func(template) template = Template(template) xml_result = template.render(context) filename = os.path.join(tmpdir, f_to_process) result_fd = open(filename, "w") result_fd.write(smart_str(xml_result)) result_fd.close() lowlevel_fd, tmpfile = tempfile.mkstemp(suffix=".odt", dir=WEBODT_TMP_DIR) os.close(lowlevel_fd) tmpzipfile = zipfile.ZipFile(tmpfile, "w") for root, _, files in os.walk(tmpdir): for fn in files: path = os.path.join(root, fn) os.utime(path, (self._fake_timestamp, self._fake_timestamp)) fn = os.path.relpath(path, tmpdir) tmpzipfile.write(path, fn) tmpzipfile.close() # remove directory tree shutil.rmtree(tmpdir) # return ODF document return ODFDocument(tmpfile, delete_on_close=delete_on_close)
def render(self, context, delete_on_close=True): """ Return rendered ODF (webodt.ODFDocument instance)""" # create temp output directory tmpdir = tempfile.mkdtemp() self.handler.unpack(tmpdir) # store updated content.xml template_content = self.get_content_xml() for preprocess_func in list_preprocessors(self.preprocessors): template_content = preprocess_func(template_content) self.template_content = template_content template = Template(template_content) content_xml = template.render(context) content_filename = os.path.join(tmpdir, 'content.xml') content_fd = open(content_filename, 'w') content_fd.write(smart_str(content_xml)) content_fd.close() # create .odt file _, tmpfile = tempfile.mkstemp(suffix='.odt', dir=WEBODT_TMP_DIR) tmpzipfile = zipfile.ZipFile(tmpfile, 'w') for root, _, files in os.walk(tmpdir): for fn in files: path = os.path.join(root, fn) os.utime(path, (self._fake_timestamp, self._fake_timestamp)) fn = os.path.relpath(path, tmpdir) tmpzipfile.write(path, fn) tmpzipfile.close() # remove directory tree shutil.rmtree(tmpdir) # return ODF document return ODFDocument(tmpfile, delete_on_close=delete_on_close)
def render(self, context, delete_on_close=True): """ Return rendered ODF (webodt.ODFDocument instance)""" # create temp output directory tmpdir = tempfile.mkdtemp() self.handler.unpack(tmpdir) # store updated content.xml for f_to_process in self.get_files_to_process(): template = self.get_file(f_to_process) for preprocess_func in list_preprocessors(self.preprocessors): template = preprocess_func(template) template = Template(template) xml_result = template.render(context) # process images MY_NAMESPACES = { 'draw': 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0', 'xlink': 'http://www.w3.org/1999/xlink', 'svg': ( 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0') } # make sure we use utf-8 parser utf8_parser = etree.XMLParser(encoding='utf-8') tree = etree.fromstring(xml_result, utf8_parser) images = tree.findall( './/{%s}image' % MY_NAMESPACES['draw'], namespaces=MY_NAMESPACES) for image_el in images: old_image_file = image_el.attrib[ '{%s}href' % MY_NAMESPACES['xlink'] ] new_image_file = image_el.getparent().find( '{%s}title' % MY_NAMESPACES['svg'], namespaces=MY_NAMESPACES ).text # get the text of the svg:title element # replace the old image file shutil.copyfile(new_image_file, os.path.join(tmpdir, old_image_file)) filename = os.path.join(tmpdir, f_to_process) result_fd = open(filename, 'w') result_fd.write(smart_str(xml_result)) result_fd.close() lowlevel_fd, tmpfile = tempfile.mkstemp(suffix='.odt', dir=WEBODT_TMP_DIR) os.close(lowlevel_fd) tmpzipfile = zipfile.ZipFile(tmpfile, 'w') for root, _, files in os.walk(tmpdir): for fn in files: path = os.path.join(root, fn) os.utime(path, (self._fake_timestamp, self._fake_timestamp)) fn = os.path.relpath(path, tmpdir) tmpzipfile.write(path, fn) tmpzipfile.close() # remove directory tree shutil.rmtree(tmpdir) # return ODF document return ODFDocument(tmpfile, delete_on_close=delete_on_close)