Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Font.name_far_east #154

Open
T4erg opened this issue Mar 5, 2015 · 8 comments

Comments

@T4erg
Copy link

commented Mar 5, 2015

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.

@scanny

This comment has been minimized.

Copy link
Contributor

commented Mar 6, 2015

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.

@T4erg

This comment has been minimized.

Copy link
Author

commented Mar 6, 2015

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.

@scanny

This comment has been minimized.

Copy link
Contributor

commented Mar 7, 2015

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.

@scanny scanny changed the title How to use a custome font feature: Font.name_far_east Mar 7, 2015
@T4erg

This comment has been minimized.

Copy link
Author

commented Mar 7, 2015

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>
@scanny

This comment has been minimized.

Copy link
Contributor

commented Mar 7, 2015

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')
@T4erg

This comment has been minimized.

Copy link
Author

commented Mar 8, 2015

Thanks a lot. It works.
But the last line should be as follows:

r.rPr.rFonts.set(qn('w:eastAsia'), 'Adobe 楷体 Std R')
@T4erg T4erg closed this Mar 8, 2015
@scanny

This comment has been minimized.

Copy link
Contributor

commented Mar 9, 2015

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.

@monkey2000

This comment has been minimized.

Copy link

commented Feb 2, 2017

Hey @scanny , this problem can be easily patched by PR #329 . Unfortunately, @leonzhu211 forgot to update the unit test. More info at Travis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.