Modify PR to be more generic

This commit is contained in:
Eric Lapouyade 2022-08-04 10:27:02 +02:00
parent ea987f9e38
commit 4857a6ed72
5 changed files with 30 additions and 12 deletions

View File

@ -1,3 +1,7 @@
0.16.4 (2022-08-04)
-------------------
- Regional fonts for RichText
0.16.3 (2022-07-14)
-------------------
- fix #448

View File

@ -223,10 +223,16 @@ RichText
When you use ``{{ <var> }}`` tag in your template, it will be replaced by the string contained within `var` variable.
BUT it will keep the current style.
If you want to add dynamically changeable style, you have to use both : the ``{{r <var> }}`` tag AND a ``RichText`` object within `var` variable.
You can change color, bold, italic, size and so on, but the best way is to use Microsoft Word to define your own *character* style
You can change color, bold, italic, size, font and so on, but the best way is to use Microsoft Word to define your own *character* style
( Home tab -> modify style -> manage style button -> New style, select Character style in the form ), see example in `tests/richtext.py`
Instead of using ``RichText()``, one can use its shortcut : ``R()``
There is a specific case for font: if your font is not displayed correctly, it may be because it is defined
only for a region. To know your region, it requires a little work by analyzing the document.xml inside the docx template (this is a zip file).
To specify a region, you have to prefix your font name this that region and a column::
ch = RichText('测试TEST', font='eastAsia:微软雅黑')
**Important** : When you use ``{{r }}`` it removes the current character styling from your docx template, this means that if
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).

View File

@ -4,7 +4,7 @@ Created : 2015-03-12
@author: Eric Lapouyade
"""
__version__ = '0.16.3'
__version__ = '0.16.4'
# flake8: noqa
from .inline_image import InlineImage

View File

@ -79,8 +79,14 @@ class RichText(object):
if strike:
prop += u'<w:strike/>'
if font:
prop += (u'<w:rFonts w:ascii="{font}" w:hAnsi="{font}" w:cs="{font}" w:eastAsia="{font}"/>'
.format(font=font))
regional_font = u''
if ':' in font:
region, font = font.split(':', 1)
regional_font = u' w:{region}="{font}"'.format(font=font, region=region)
prop += (
u'<w:rFonts w:ascii="{font}" w:hAnsi="{font}" w:cs="{font}"{regional_font}/>'
.format(font=font, regional_font=regional_font)
)
xml = u'<w:r>'
if prop:

View File

@ -1,17 +1,19 @@
# -*- coding: utf-8 -*-
'''
"""
Created : 2022-08-03
@author: Dongfang Song
'''
"""
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('templates/richtext_eastAsia_tpl.docx')
rt = RichText('测试TEST', font = 'Microsoft YaHei')
ch = RichText('测试TEST', font = '微软雅黑')
sun = RichText('测试TEST', font = 'SimSun')
rt = RichText('测试TEST', font='eastAsia:Microsoft YaHei')
ch = RichText('测试TEST', font='eastAsia:微软雅黑')
sun = RichText('测试TEST', font='eastAsia:SimSun')
context = {
'example' : rt,
'Chinese' : ch,
'simsun' : sun,
'example': rt,
'Chinese': ch,
'simsun': sun,
}
tpl.render(context)