I find in there http://python-docx.readthedocs.org/en/latest/api/text.html#docx.text.run.Font
the font object has a name attribute, and i want to use a custome font. Where should i put the font's file(.otf)?
run.font.name = "Adobe 楷体 Std R"#this the font's name. Should i use the font's file name to replace the font's name ?
I try use it, but useless. What should I do?
Thanks.
The name that Word uses for some fonts is unpredictable. In general, if it doesn't work the way it's spelled in the Word UI, the most direct approach is to use Word to apply the font to some text in a very small document (like a single word in a single paragraph), save the file, and then use opc-diag to examine the document.xml part.
$ opc browse test-doc.docx document.xml
The proper name to use will appear there, something like this:
<w:r w:rsidRPr="00392E92">
<w:rPr>
<w:rFonts w:ascii="ヒラギノ角ゴ ProN W3" w:eastAsia="ヒラギノ角ゴ ProN W3" w:hAnsi="ヒラギノ角ゴ ProN W3"/>
</w:rPr>
<w:t>Foobar.</w:t>
</w:r>
If they differ, you want the one in w:ascii and w:hAnsi. I believe python-docx sets them both to the same value.
I find the 'w:ascii' and use the name. It still doesn't work. My code as follow:
run = paragraphs[3].add_run(namea1)
run.bold = True #It works.
run.font.size = Pt(19) #It works.
run.font.name = "Adobe 楷体 Std R" #It doesn't work.
I will try other way to solve it.
Thanks again.
What do you find when you compare the XML produced by python-docx and that produced by Word? It could be that Word is using the font in the w:eastAsia attribute for text in Asian unicode character groups.
Excited! You remind me!
<w:rFonts w:ascii="Adobe 楷体 Std R" w:eastAsia="Adobe 楷体 Std R" w:hAnsi="宋体" w:hint="eastAsia"/>
The w:ascii and w:eastAsia are correct But the w:hAnsi was INCORRECT.
This line was appear before the WORDS that I want to change.
The followe line was the WORDS and the attribute are correct. But didn't have the w:hAnsi attribute, But display INCORRECT.
<w:r>
<w:rPr>
<w:rFonts w:ascii="CORRECT" w:hAnsi="CORRECT"/>
<w:b/>
<w:sz w:val="38"/>
</w:rPr>
<w:t>*THE WORDS THAT I WANT TO CHANGE*</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:ascii="CORRECT" w:hAnsi="CORRECT"/>
<w:b/>
<w:sz w:val="38"/>
</w:rPr>
<w:t xml:space="preserve"> ...
</w:r>
You should be able to add a w:eastAsia attribute like this:
from docx.oxml.ns import qn
run = paragraphs[3].add_run(namea1)
run.font.name = "Adobe 楷体 Std R"
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), 'Adobe 楷体 Std R')
Thanks a lot. It works.
But the last line should be as follows:
r.rPr.rFonts.set(qn('w:eastAsia'), 'Adobe 楷体 Std R')
Ah, yes, of course, thanks @Taerg-L, I've corrected that.
I'm reopening this issue to act as a feature request to add Font.name_far_east to the API.
Hey @scanny , this problem can be easily patched by PR #329 . Unfortunately, @leonzhu211 forgot to update the unit test. More info at Travis.
This issue can be fixed by PR https://github.com/python-openxml/python-docx/pull/781, which updates the unit test code for PR https://github.com/python-openxml/python-docx/pull/329 . @scanny
Most helpful comment
You should be able to add a
w:eastAsiaattribute like this: