简体   繁体   English

python-docx document-styles字体变为标题样式失败

[英]python-docx document-styles font failed to the Heading style

I have used python-docx in my django project to MS-word report, and I modified the Normal content style successfully, I have got the '宋体' font: 我在django项目中使用python-docx来生成MS-word报告,并且成功修改了Normal内容样式,并获得了“宋体”字体:

            document.styles['Normal'].font.name = u'宋体'
            report.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
            document.styles['Normal'].font.size = Pt(9)

But when I use it to the Heading content, the Heading font did not change. 但是当我将其用于标题内容时,标题字体没有更改。 no any failed shows.Curiously, the Heading size was changed. 没有任何失败的显示。奇怪的是,标题大小已更改。 Why this happened, how to solve this? 为什么会这样,如何解决呢?

            document.add_paragraph('1.清单', 'Heading 1')
            document.add_paragraph('设备:', 'Heading 2')

            document.styles['Heading 1'].font.name = u'宋体'
            document.styles['Heading 1']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
            document.styles['Heading 1'].font.size = Pt(12)
            document.styles['Heading 2'].font.name = u'宋体'
            document.styles['Heading 2']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
            document.styles['Heading 2'].font.size = Pt(10.5)

And I want to konw how to modify Chinese and English fonts respectively? 我想知道如何分别修改中文和英文字体?
Thanks! 谢谢!

I used a simple solution, I created a new style 我使用了一个简单的解决方案,创建了一种新样式

            style_T1 = report.styles.add_style('T1', WD_STYLE_TYPE.PARAGRAPH)
            style_T1.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

It worked! 有效! So I created several styles,and replaced 'Heading 1','Heading 2'... 所以我创建了几种样式,并替换了“标题1”,“标题2” ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM