From 4857a6ed72c376f020251d669b2c56413bf0a8cc Mon Sep 17 00:00:00 2001 From: Eric Lapouyade Date: Thu, 4 Aug 2022 10:27:02 +0200 Subject: [PATCH] Modify PR to be more generic --- CHANGES.rst | 4 ++++ docs/index.rst | 8 +++++++- docxtpl/__init__.py | 2 +- docxtpl/richtext.py | 10 ++++++++-- tests/richtext_eastAsia.py | 18 ++++++++++-------- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 4fee050..336b7a6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,7 @@ +0.16.4 (2022-08-04) +------------------- +- Regional fonts for RichText + 0.16.3 (2022-07-14) ------------------- - fix #448 diff --git a/docs/index.rst b/docs/index.rst index 2989c92..ff01e28 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -223,10 +223,16 @@ RichText When you use ``{{ }}`` 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 }}`` 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). diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 94fc1fe..732521c 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -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 diff --git a/docxtpl/richtext.py b/docxtpl/richtext.py index 80b567e..fc2a488 100644 --- a/docxtpl/richtext.py +++ b/docxtpl/richtext.py @@ -79,8 +79,14 @@ class RichText(object): if strike: prop += u'' if font: - prop += (u'' - .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'' + .format(font=font, regional_font=regional_font) + ) xml = u'' if prop: diff --git a/tests/richtext_eastAsia.py b/tests/richtext_eastAsia.py index 168ad72..2a61074 100644 --- a/tests/richtext_eastAsia.py +++ b/tests/richtext_eastAsia.py @@ -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)