在 python-docx 中设置段落字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27884703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set paragraph font in python-docx
提问by MavWolverine
I am using python-docx 0.7.6.
我正在使用 python-docx 0.7.6。
I can't seem to be able to figure out how to set font family and size for a certain paragraph.
我似乎无法弄清楚如何为某个段落设置字体系列和大小。
There is .style
property but style="Times New Roman"
doesn't work.
有.style
属性,但style="Times New Roman"
不起作用。
Can somebody please point me to an example?
有人可以指出我的例子吗?
Thanks.
谢谢。
采纳答案by MavWolverine
Support for run styles has been added in latest version of python-docx
在最新版本的 python-docx 中添加了对运行样式的支持
回答by scanny
The documentation for python-docx is here: http://python-docx.readthedocs.org/en/latest/
python-docx 的文档在这里:http: //python-docx.readthedocs.org/en/latest/
The styles that are available in the default template are listed here: http://python-docx.readthedocs.org/en/latest/user/styles.html
此处列出了默认模板中可用的样式:http: //python-docx.readthedocs.org/en/latest/user/styles.html
In your example above you used a typeface name ("Times New Roman") instead of a style id. If you use "Heading1" for example, that would change the look of the font, among other things, since it's a paragraph style.
在上面的示例中,您使用了字体名称(“Times New Roman”)而不是样式 ID。例如,如果您使用“Heading1”,这将改变字体的外观,因为它是一种段落样式。
At present there is no API for directly applying a typeface name or font size to text in python-docx, although that and more is coming in the next release, probably within a month. In the meantime, you can define styles for the paragraph and character settings you want and apply those styles. Using styles is the recommended way to apply formatting in Word, similar to how CSS is the recommended way to apply formatting to HTML.
目前还没有用于在 python-docx 中直接将字体名称或字体大小应用于文本的 API,尽管在下一个版本中可能会在一个月内发布更多内容。同时,您可以为所需的段落和字符设置定义样式并应用这些样式。使用样式是在 Word 中应用格式的推荐方式,类似于 CSS 是将格式应用于 HTML 的推荐方式。
回答by Alex Nies
After reading through the API documentationI was able to figure out how to create my own style and apply it. You could create a paragraph style object the same way by changing this code to use WD_STYLE_TYPE.PARAGRAPH. Something that took me a minute to figure out was the objects and at what level they are applied so just make sure you understand that clearly. Something I found counter intuitive is that you define the styles properties after its creation.
通读API 文档后,我能够弄清楚如何创建自己的样式并应用它。您可以通过将此代码更改为使用 WD_STYLE_TYPE.PARAGRAPH 以相同的方式创建段落样式对象。我花了一分钟才弄清楚的是对象以及它们在什么级别上应用,所以请确保您清楚地了解这一点。我发现与直觉相反的是,您在创建样式属性后定义它。
This is how I created a character level style object.
这就是我创建角色级别样式对象的方式。
document = Document(path to word document)
# Creates a character level style Object ("CommentsStyle") then defines its parameters
# 创建一个字符级样式对象(“CommentsStyle”)然后定义它的参数
obj_styles = document.styles
obj_charstyle = obj_styles.add_style('CommentsStyle', WD_STYLE_TYPE.CHARACTER)
obj_font = obj_charstyle.font
obj_font.size = Pt(10)
obj_font.name = 'Times New Roman'
This is how I applied the style to a run.
这就是我将样式应用于跑步的方式。
paragraph.add_run(any string variable, style = 'CommentsStyle').bold = True
回答by Zenadix
This is how to set the Normal
style to font Arial
and size 10pt
.
这是将Normal
样式设置为 fontArial
和 size 的方法10pt
。
from docx.shared import Pt
style = document.styles['Normal']
font = style.font
font.name = 'Arial'
font.size = Pt(10)
And this is how to apply it to a paragraph
.
这就是如何将它应用于paragraph
.
paragraph.style = document.styles['Normal']
Using the current version of python-docx (0.8.5).
使用当前版本的 python-docx (0.8.5)。