How to use the mailmerge.MailMerge function in mailmerge

To help you get started, we’ve selected a few mailmerge 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 Bouke / docx-mailmerge / tests / test_issue8.py View on Github external
def test(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_issue8.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'testfield'})
github Bouke / docx-mailmerge / tests / test_winword2010.py View on Github external
def test(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_winword2010.docx')) as document:
            self.assertEqual(document.get_merge_fields(),
                             set(['Titel', 'Voornaam', 'Achternaam',
                                  'Adresregel_1', 'Postcode', 'Plaats',
                                  'Provincie', 'Land_of_regio']))

            document.merge(Voornaam='Bouke', Achternaam='Haarsma',
                           Land_of_regio='The Netherlands', Provincie=None,
                           Postcode='9723 ZA', Plaats='Groningen',
                           Adresregel_1='Helperpark 278d\nP.O. Box', Titel='dhr.')

            with tempfile.NamedTemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring(
            ''  # noqa
              ''
github Bouke / docx-mailmerge / tests / test_macword2011.py View on Github external
def test(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_macword2011.docx')) as document:
            self.assertEqual(document.get_merge_fields(),
                             set(['first_name', 'last_name', 'country', 'state',
                                  'postal_code', 'date', 'address_line', 'city']))

            document.merge(first_name='Bouke', last_name='Haarsma',
                           country='The Netherlands', state=None,
                           postal_code='9723 ZA', city='Groningen',
                           address_line='Helperpark 278d', date='May 22nd, 2013')

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring(
            ''
github Bouke / docx-mailmerge / tests / test_merge_pages.py View on Github external
def test_pages_with_multiple_pages(self):
        """
        This also tests that merge_pages produces a multiple paged document,
        however this template already contains two pages. So the result should
        be 3 * 2 pages.
        """
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_pages_paged.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_pages([
                {'fieldname': "xyz"},
                {'fieldname': "abc"},
                {'fieldname': "2b v ~2b"},
            ])

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('This is a template for the xyz merge_list test case, page 1This is a template for the xyz merge_list test case, page 2This is a template for the abc merge_list test case, page 1This is a template for the abc merge_list test case, page 2This is a template for the 2b v ~2b merge_list test case, page 1This is a template for the 2b v ~2b merge_list test case, page 2')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())
github Bouke / docx-mailmerge / tests / test_merge_pages.py View on Github external
def test_pages(self):
        with MailMerge(path.join(path.dirname(__file__), 'test_merge_pages.docx')) as document:
            self.assertEqual(document.get_merge_fields(), {'fieldname'})

            document.merge_pages([
                {'fieldname': "xyz"},
                {'fieldname': "abc"},
                {'fieldname': "2b v ~2b"},
            ])

            with tempfile.TemporaryFile() as outfile:
                document.write(outfile)

        expected_tree = etree.fromstring('This is a template for the xyz merge_list test caseThis is a template for the abc merge_list test caseThis is a template for the 2b v ~2b merge_list test case')  # noqa

        self.assert_equal_tree(expected_tree, get_document_body_part(document).getroot())
github Bouke / docx-mailmerge / tests / test_merge_table_rows.py View on Github external
def setUp(self):
        self.document = MailMerge(path.join(path.dirname(__file__), 'test_merge_table_rows.docx'))
        self.expected_tree = etree.fromstring('GradesBouke Haarsma received the grades for  in the table below.Class CodeClass NameGradeECON101Economics 101AECONADVEconomics AdvancedBOPRESOperations ResearchATHESISFinal thesisA')  # noqa
github Bouke / docx-mailmerge / tests / test_merge_table_multipart.py View on Github external
def setUp(self):
        self.document = MailMerge(path.join(path.dirname(__file__), 'test_merge_table_multipart.docx'))
        self.expected_xml = 'GradesBouke Haarsma received the grades for  in the table below.Class CodeClass NameGradeECON101Economics 101AECONADVEconomics AdvancedBOPRESOperations ResearchATHESISFinal thesisA'
        self.expected_tree = etree.fromstring(self.expected_xml)
github adfinis-sygroup / document-merge-service / document_merge_service / api / engines.py View on Github external
def merge(self, data, buf):
        with MailMerge(self.template) as document:
            document.merge(**data)
            document.write(buf)
            return buf
github adfinis-sygroup / document-merge-service / document_merge_service / api / engines.py View on Github external
def validate_template_syntax(self, available_placeholders=None, sample_data=None):
        document = MailMerge(self.template)
        # syntax can't be invalid as it's validated by office
        # suites. However we need to have *some* placeholders
        self.template.seek(0)
        used_placeholders = self._get_placeholders(document)
        self.validate_available_placeholders(
            used_placeholders=used_placeholders,
            available_placeholders=available_placeholders,
        )

        if sample_data:
            buffer = io.BytesIO()
            self.merge(sample_data, buffer)
            self.template.seek(0)

        if not len(used_placeholders):
            raise exceptions.ValidationError("Template has no merge fields")