You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried set set Chinese character font typeface, but failed. it works for English characters, but not for Chinese characters although they are at the same line.
from docx import Document
from docx.shared import Pt
document = Document()
p = document.add_paragraph('A plain paragraph having some ')
run = p.add_run("\n中文字体设置。Character formatting is applied at the Run level. Examples include font typeface and size, bold, italic, and underline.")
font = run.font
font.name = '宋体' #simSong
font.size = Pt(20)
a related question: what if we want use New Times Roman for English Characters, but '宋体' #simSong for Chinese?
In WORD, such a style can be made. But how can we do that in PYTHON-DOCX?
Have you solve the problem ?
the following code solves 2 problems:
use Chinese font
use font 1 for Chinese, but font 2 for English in one paragraph
fromdocximportDocumentfromdocx.sharedimportPtfromdocx.oxml.nsimportqndocument=Document()
p=document.add_paragraph()
run=p.add_run("这是新添加的字符Hello World。又切换到中文1234567,./:_")
# 必须有这一句,否则下面设置中文字体时 “AttributeError: 'NoneType' object has no attribute 'set'”# 同时,这一句也指定了英文、数字、英文标点的字体run.font.name='Times New Roman'run.font.size=Pt(10.5) # 五号run._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
run.font.size=Pt(10.5) # 五号document.save("docx中文、英文设置不同字体.docx")
I managed to set the fontName of Chinese characters by following #154 (comment); In my case , Chinese characters fontName not correctly set, but number characters are correct in the same paragraph; So after adding the following code, it works on Chinese characters; Might be some attributes lost on the xml; Make sure to use the correct font name;
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑')
r.rPr.rFonts.set(qn('w:hAnsi'), '微软雅黑')
r.rPr.rFonts.set(qn('w:ascii'), '微软雅黑')
Activity
scanny commentedon Jan 9, 2017
Check out this existing issue and see of that solves your problem: #154
retsyo commentedon Mar 2, 2017
a related question: what if we want use
New Times Romanfor English Characters, but'宋体' #simSongfor Chinese?In WORD, such a style can be made. But how can we do that in PYTHON-DOCX?
liumengyu96 commentedon Dec 27, 2018
Have you solve the problem ?
retsyo commentedon Dec 29, 2018
I give up. If necessary I will use win32com to tweak the details further
joaokho commentedon Sep 12, 2019
Bugs I think it's never success, it's success to change to font name before the chinese character, but on chinese character always using 'MS 明朝' font.
retsyo commentedon Apr 29, 2023
the following code solves 2 problems:
tcztzy commentedon Aug 30, 2023
If you want to set font family globally instead of per paragraph, you should try the codes following.
fatpandaria commentedon Nov 24, 2024
I managed to set the fontName of Chinese characters by following #154 (comment); In my case , Chinese characters fontName not correctly set, but number characters are correct in the same paragraph; So after adding the following code, it works on Chinese characters; Might be some attributes lost on the xml; Make sure to use the correct font name;