How to use the docxtpl.__init__.RichText function in docxtpl

To help you get started, we’ve selected a few docxtpl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github elapouya / python-docx-template / docxtpl / __init__.py View on Github external
xml = ( u'%s'
                    % (url_id, xml) )
        self.xml += xml


    def __unicode__(self):
        return self.xml

    def __str__(self):
        return self.xml

    def __html__(self):
        return self.xml


R = RichText


class Listing(object):
    r"""class to manage \n and \a without to use RichText,
    by this way you keep the current template styling

    use {{ mylisting }} in your template and
    context={ mylisting:Listing(the_listing_with_newlines) }
    """
    def __init__(self, text):
        # If not a string : cast to string (ex: int, dict etc...)
        if not isinstance(text, (six.text_type, six.binary_type)):
            text = six.text_type(text)
        self.xml = (escape(text)
                    .replace('\n', NEWLINE_XML)
                    .replace('\a', NEWPARAGRAPH_XML)
github elapouya / python-docx-template / docxtpl / __init__.py View on Github external
def add(self, text, style=None,
                        color=None,
                        highlight=None,
                        size=None,
                        subscript=None,
                        superscript=None,
                        bold=False,
                        italic=False,
                        underline=False,
                        strike=False,
                        font=None,
                        url_id=None):

        # If a RichText is added
        if isinstance(text, RichText):
            self.xml += text.xml
            return

        # If not a string : cast to string (ex: int, dict etc...)
        if not isinstance(text, (six.text_type, six.binary_type)):
            text = six.text_type(text)
        if not isinstance(text, six.text_type):
            text = text.decode('utf-8',errors='ignore')
        text = (escape(text)
                .replace('\n', NEWLINE_XML)
                .replace('\a', NEWPARAGRAPH_XML)
                .replace('\t', TAB_XML)
                .replace('\f', PAGE_BREAK))

        prop = u''

docxtpl

Python docx template engine

LGPL-2.1
Latest version published 4 months ago

Package Health Score

74 / 100
Full package analysis