diff --git a/CHANGES.rst b/CHANGES.rst index aa0782d..c2c7316 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,7 @@ +0.5.0 (2018-08-03) +------------------- +- An hyperlink can now be used in RichText + 0.4.13 (2018-06-21) ------------------- - Subdocument can now be based on an existing docx diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..feed548 --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +url = "https://pypi.python.org/simple" +verify_ssl = true +name = "pypi" + +[dev-packages] +"e1839a8" = {path = ".", editable = true} + +[packages] + +[requires] +python_version = "2.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..8bbc432 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,82 @@ +{ + "_meta": { + "hash": { + "sha256": "0259bf6a7ccb55049c4e7521b2c644cdc4f80a098d8f1956465b308bc4e897fd" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "2.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.python.org/simple", + "verify_ssl": true + } + ] + }, + "default": {}, + "develop": { + "e1839a8": { + "editable": true, + "path": "." + }, + "jinja2": { + "hashes": [ + "sha256:74c935a1b8bb9a3947c50a54766a969d4846290e1e788ea44c1392163723c3bd", + "sha256:f84be1bb0040caca4cea721fcbbbbd61f9be9464ca236387158b0feea01914a4" + ], + "version": "==2.10" + }, + "lxml": { + "hashes": [ + "sha256:0941f4313208c07734410414d8308812b044fd3fb98573454e3d3a0d2e201f3d", + "sha256:0b18890aa5730f9d847bc5469e8820f782d72af9985a15a7552109a86b01c113", + "sha256:21f427945f612ac75576632b1bb8c21233393c961f2da890d7be3927a4b6085f", + "sha256:24cf6f622a4d49851afcf63ac4f0f3419754d4e98a7a548ab48dd03c635d9bd3", + "sha256:2dc6705486b8abee1af9e2a3761e30a3cb19e8276f20ca7e137ee6611b93707c", + "sha256:2e43b2e5b7d2b9abe6e0301eef2c2c122ab45152b968910eae68bdee2c4cfae0", + "sha256:329a6d8b6d36f7d6f8b6c6a1db3b2c40f7e30a19d3caf62023c9d6a677c1b5e1", + "sha256:423cde55430a348bda6f1021faad7235c2a95a6bdb749e34824e5758f755817a", + "sha256:4651ea05939374cfb5fe87aab5271ed38c31ea47997e17ec3834b75b94bd9f15", + "sha256:4be3bbfb2968d7da6e5c2cd4104fc5ec1caf9c0794f6cae724da5a53b4d9f5a3", + "sha256:622f7e40faef13d232fb52003661f2764ce6cdef3edb0a59af7c1559e4cc36d1", + "sha256:664dfd4384d886b239ef0d7ee5cff2b463831079d250528b10e394a322f141f9", + "sha256:697c0f58ac637b11991a1bc92e07c34da4a72e2eda34d317d2c1c47e2f24c1b3", + "sha256:6ec908b4c8a4faa7fe1a0080768e2ce733f268b287dfefb723273fb34141475f", + "sha256:7ec3fe795582b75bb49bb1685ffc462dbe38d74312dac07ce386671a28b5316b", + "sha256:8c39babd923c431dcf1e5874c0f778d3a5c745a62c3a9b6bd755efd489ee8a1d", + "sha256:949ca5bc56d6cb73d956f4862ba06ad3c5d2808eac76304284f53ae0c8b2334a", + "sha256:9f0daddeefb0791a600e6195441910bdf01eac470be596b9467e6122b51239a6", + "sha256:a359893b01c30e949eae0e8a85671a593364c9f0b8162afe0cb97317af0953bf", + "sha256:ad5d5d8efed59e6b1d4c50c1eac59fb6ecec91b2073676af1e15fc4d43e9b6c5", + "sha256:bc1a36f95a6b3667c09b34995fc3a46a82e4cf0dc3e7ab281e4c77b15bd7af05", + "sha256:be37b3f55b6d7d923f43bf74c356fc1878eb36e28505f38e198cb432c19c7b1a", + "sha256:c45bca5e544eb75f7500ffd730df72922eb878a2f0213b0dc5a5f357ded3a85d", + "sha256:ccee7ebbb4735ebc341d347fca9ee09f2fa6c0580528c1414bc4e1d31372835c", + "sha256:dc62c0840b2fc7753550b40405532a3e125c0d3761f34af948873393aa688160", + "sha256:f7d9d5aa1c7e54167f1a3cba36b5c52c7c540f30952c9bd7d9302a1eda318424" + ], + "version": "==4.2.3" + }, + "markupsafe": { + "hashes": [ + "sha256:a6be69091dac236ea9c6bc7d012beab42010fa914c459791d627dad4910eb665" + ], + "version": "==1.0" + }, + "python-docx": { + "hashes": [ + "sha256:55ece6fd4a4fa3389909fa0e51400fce428e1fb6f6ef3599cbba31673441f184" + ], + "version": "==0.8.6" + }, + "six": { + "hashes": [ + "sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9", + "sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb" + ], + "version": "==1.11.0" + } + } +} diff --git a/docs/index.rst b/docs/index.rst index 0bd9af8..7d6a1b1 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -126,6 +126,17 @@ Instead of using ``RichText()``, one can use its shortcut : ``R()`` you do not specify a style in ``RichText()``, the style will go back to a microsoft word default style. This will affect only character styles, not the paragraph styles (MSWord manages this 2 kind of styles). +Hyperlink with RichText +....................... + +You can add an hyperlink to a text by using a Richtext with this syntax :: + + tpl=DocxTemplate('your_template.docx') + rt = RichText('You can add an hyperlink, here to ') + rt.add('google',url_id=tpl.build_url_id('http://google.com')) + +Put ``rt`` in your context, then use ``{{r rt}}`` in your template + Inline image ------------ diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 3ccfbf1..99d28cc 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -5,7 +5,7 @@ Created : 2015-03-12 @author: Eric Lapouyade ''' -__version__ = '0.4.13' +__version__ = '0.5.0' from lxml import etree from docx import Document @@ -350,6 +350,10 @@ class DocxTemplate(object): return part_map + def build_url_id(self,url): + return self.docx._part.relate_to(url, REL_TYPE.HYPERLINK, + is_external=True) + def save(self,filename,*args,**kwargs): self.pre_processing() self.docx.save(filename,*args,**kwargs) @@ -382,7 +386,6 @@ class Subdoc(object): def __html__(self): return self._get_xml() - class RichText(object): """ class to generate Rich Text when using templates variables @@ -401,7 +404,8 @@ class RichText(object): italic=False, underline=False, strike=False, - font=None): + font=None, + url_id=None): if not isinstance(text, six.text_type): @@ -436,10 +440,15 @@ class RichText(object): if font: prop += u''.format(font=font) - self.xml += u'' + + xml = u'' if prop: - self.xml += u'%s' % prop - self.xml += u'%s' % text + xml += u'%s' % prop + xml += u'%s' % text + if url_id: + xml = u'%s' % (url_id, xml) + self.xml += xml + def __unicode__(self): return self.xml diff --git a/tests/richtext.py b/tests/richtext.py index 881df08..e807eb1 100644 --- a/tests/richtext.py +++ b/tests/richtext.py @@ -22,7 +22,9 @@ rt.add('some small', size=14) rt.add(' or ') rt.add('big', size=60) rt.add(' text.') -rt.add(' Et voilĂ  ! ') +rt.add('\nYou can add an hyperlink, here to ') +rt.add('google',url_id=tpl.build_url_id('http://google.com')) +rt.add('\nEt voilĂ  ! ') rt.add('\n1st line') rt.add('\n2nd line') rt.add('\n3rd line') diff --git a/tests/test_files/cellbg.docx b/tests/test_files/cellbg.docx index 6df5f66..943b68e 100644 Binary files a/tests/test_files/cellbg.docx and b/tests/test_files/cellbg.docx differ diff --git a/tests/test_files/dynamic_table.docx b/tests/test_files/dynamic_table.docx index 4893b79..7c472a9 100644 Binary files a/tests/test_files/dynamic_table.docx and b/tests/test_files/dynamic_table.docx differ diff --git a/tests/test_files/embedded.docx b/tests/test_files/embedded.docx index 4da2ceb..6273ce9 100644 Binary files a/tests/test_files/embedded.docx and b/tests/test_files/embedded.docx differ diff --git a/tests/test_files/embedded_embedded_docx.docx b/tests/test_files/embedded_embedded_docx.docx index e81415c..c1dac2c 100644 Binary files a/tests/test_files/embedded_embedded_docx.docx and b/tests/test_files/embedded_embedded_docx.docx differ diff --git a/tests/test_files/escape.docx b/tests/test_files/escape.docx index 1a4ca52..2ab1222 100644 Binary files a/tests/test_files/escape.docx and b/tests/test_files/escape.docx differ diff --git a/tests/test_files/header_footer.docx b/tests/test_files/header_footer.docx index 82b354b..cf498f4 100644 Binary files a/tests/test_files/header_footer.docx and b/tests/test_files/header_footer.docx differ diff --git a/tests/test_files/header_footer_entities.docx b/tests/test_files/header_footer_entities.docx index 86e9822..f72c3e1 100644 Binary files a/tests/test_files/header_footer_entities.docx and b/tests/test_files/header_footer_entities.docx differ diff --git a/tests/test_files/header_footer_image.docx b/tests/test_files/header_footer_image.docx index 2983425..292d814 100644 Binary files a/tests/test_files/header_footer_image.docx and b/tests/test_files/header_footer_image.docx differ diff --git a/tests/test_files/header_footer_utf8.docx b/tests/test_files/header_footer_utf8.docx index 9ff0a88..79bbd98 100644 Binary files a/tests/test_files/header_footer_utf8.docx and b/tests/test_files/header_footer_utf8.docx differ diff --git a/tests/test_files/inline_image.docx b/tests/test_files/inline_image.docx index 5fdaac3..0594ad0 100644 Binary files a/tests/test_files/inline_image.docx and b/tests/test_files/inline_image.docx differ diff --git a/tests/test_files/nested_for.docx b/tests/test_files/nested_for.docx index 6da0ff7..179db6b 100644 Binary files a/tests/test_files/nested_for.docx and b/tests/test_files/nested_for.docx differ diff --git a/tests/test_files/order.docx b/tests/test_files/order.docx index 389c3be..cd2d952 100644 Binary files a/tests/test_files/order.docx and b/tests/test_files/order.docx differ diff --git a/tests/test_files/replace_picture.docx b/tests/test_files/replace_picture.docx index 89a0eef..4f56eae 100644 Binary files a/tests/test_files/replace_picture.docx and b/tests/test_files/replace_picture.docx differ diff --git a/tests/test_files/richtext.docx b/tests/test_files/richtext.docx index 7891b2a..3db260c 100644 Binary files a/tests/test_files/richtext.docx and b/tests/test_files/richtext.docx differ diff --git a/tests/test_files/richtext_and_if.docx b/tests/test_files/richtext_and_if.docx index 8d70446..650df47 100644 Binary files a/tests/test_files/richtext_and_if.docx and b/tests/test_files/richtext_and_if.docx differ diff --git a/tests/test_files/richtext_tpl.docx b/tests/test_files/richtext_tpl.docx index bcf589e..6c7a250 100644 Binary files a/tests/test_files/richtext_tpl.docx and b/tests/test_files/richtext_tpl.docx differ diff --git a/tests/test_files/subdoc.docx b/tests/test_files/subdoc.docx index e4f3668..6de6526 100644 Binary files a/tests/test_files/subdoc.docx and b/tests/test_files/subdoc.docx differ diff --git a/tests/test_files/vertical_merge.docx b/tests/test_files/vertical_merge.docx index 6a5e1ab..d66fdb1 100644 Binary files a/tests/test_files/vertical_merge.docx and b/tests/test_files/vertical_merge.docx differ diff --git a/tests/test_files/word2016.docx b/tests/test_files/word2016.docx index adee333..37a1e1e 100644 Binary files a/tests/test_files/word2016.docx and b/tests/test_files/word2016.docx differ