2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python-docxで日本語用のフォントを設定する

Posted at

できること

「日本語用のフォント」を設定できるようになります
image.png

利用法

real_usecase.py
# 東アジア言語用のフォント設定をします
paragraph.runs[0].font.name_eastasia = "MS ゴシック"

設定

python-docxのファイルをいじって、東アジアの言語(いわゆるCJK Languageだと思われる)に対応させます。
私はminicondaを利用していますが、各自の環境に合わせていじるファイルの場所を特定してください

miconda3/envs//Lib/site-packages/docx/text/font.py/Font
# 以下の部分をnameのgetter/setterの後あたりに追加
    @property
    def name_eastasia(self):
        """
        Get or set the typeface name for this |Font| instance, causing the
        text it controls to appear in the named font, if a matching font is
        found. |None| indicates the typeface is inherited from the style
        hierarchy.
        """
        rPr = self._element.rPr
        if rPr is None:
            return None
        return rPr.rFonts_eastAsia

    @name_eastasia.setter
    def name_eastasia(self, value):
        rPr = self._element.get_or_add_rPr()
        rPr.rFonts_eastAsia = value
        rPr.rFonts_hint = "eastAsia"

miconda3/envs//Lib/site-packages/docx/oxml/text/font.py
# 以下の部分をrFont_hAnsiのgetter/setterの後あたりに追加
    @property
    def rFonts_eastAsia(self):
        """
        The value of `w:rFonts/@w:eastAsia` or |None| if not present.
        """
        rFonts = self.rFonts
        if rFonts is None:
            return None
        return rFonts.eastAsia

    @rFonts_eastAsia.setter
    def rFonts_eastAsia(self, value):
        if value is None and self.rFonts is None:
            return
        rFonts = self.get_or_add_rFonts()
        rFonts.eastAsia = value

    @property
    def rFonts_hint(self):
        """
        The value of `w:rFonts/@w:hint` or |None| if not present.
        """
        rFonts = self.rFonts
        if rFonts is None:
            return None
        return rFonts.hint

    @rFonts_hint.setter
    def rFonts_hint(self, value):
        if value is None and self.rFonts is None:
            return
        rFonts = self.get_or_add_rFonts()
        rFonts.hint = value
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?