Add Hyperlink in RichText

This commit is contained in:
elapouya 2018-08-03 15:26:56 +02:00
parent c9afd31320
commit 23fbe8c048
25 changed files with 127 additions and 7 deletions

View File

@ -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

12
Pipfile Normal file
View File

@ -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"

82
Pipfile.lock generated Normal file
View File

@ -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"
}
}
}

View File

@ -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
------------

View File

@ -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'<w:rFonts w:ascii="{font}" w:hAnsi="{font}" w:cs="{font}"/>'.format(font=font)
self.xml += u'<w:r>'
xml = u'<w:r>'
if prop:
self.xml += u'<w:rPr>%s</w:rPr>' % prop
self.xml += u'<w:t xml:space="preserve">%s</w:t></w:r>' % text
xml += u'<w:rPr>%s</w:rPr>' % prop
xml += u'<w:t xml:space="preserve">%s</w:t></w:r>' % text
if url_id:
xml = u'<w:hyperlink r:id="%s" w:tgtFrame="_blank">%s</w:hyperlink>' % (url_id, xml)
self.xml += xml
def __unicode__(self):
return self.xml

View File

@ -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')

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.