Merge branch 'sailingnn-master'
This commit is contained in:
commit
52d9b28a31
@ -1,3 +1,7 @@
|
||||
0.16.4 (2022-08-04)
|
||||
-------------------
|
||||
- Regional fonts for RichText
|
||||
|
||||
0.16.3 (2022-07-14)
|
||||
-------------------
|
||||
- fix #448
|
||||
|
||||
@ -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).
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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}"/>'
|
||||
.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:
|
||||
|
||||
20
tests/richtext_eastAsia.py
Normal file
20
tests/richtext_eastAsia.py
Normal file
@ -0,0 +1,20 @@
|
||||
# -*- 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='eastAsia:Microsoft YaHei')
|
||||
ch = RichText('测试TEST', font='eastAsia:微软雅黑')
|
||||
sun = RichText('测试TEST', font='eastAsia:SimSun')
|
||||
context = {
|
||||
'example': rt,
|
||||
'Chinese': ch,
|
||||
'simsun': sun,
|
||||
}
|
||||
|
||||
tpl.render(context)
|
||||
tpl.save('output/richtext_eastAsia.docx')
|
||||
BIN
tests/templates/richtext_eastAsia_tpl.docx
Normal file
BIN
tests/templates/richtext_eastAsia_tpl.docx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user