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 · 9 comments
Open

feature: Font.name_far_east #154

T4erg opened this issue Mar 5, 2015 · 9 comments
Labels

Comments

@T4erg
Copy link

T4erg 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
Copy link
Contributor

scanny 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
Copy link
Author

T4erg 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
Copy link
Contributor

scanny 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
Copy link
Author

T4erg 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
Copy link
Contributor

scanny 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
Copy link
Author

T4erg 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 as completed Mar 8, 2015
@scanny
Copy link
Contributor

scanny 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
Copy link

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

@fengdasuk19
Copy link

This issue can be fixed by PR #781, which updates the unit test code for PR #329 . @scanny

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants