在python-docx中设置段落字体

问题描述 投票:0回答:6

我正在使用 python-docx 0.7.6。

我似乎无法弄清楚如何为某个段落设置字体系列和大小。

.style
属性,但
style="Times New Roman"
不起作用。

有人可以给我举个例子吗?

谢谢。

python python-docx
6个回答
47
投票

这是如何将

Normal
样式设置为字体
Arial
和大小
10pt

from docx.shared import Pt

style = document.styles['Normal']
font = style.font
font.name = 'Arial'
font.size = Pt(10)

这就是如何将其应用到

paragraph

paragraph.style = document.styles['Normal']

使用当前版本的 python-docx (0.8.5)。


17
投票

阅读完API文档后我能够弄清楚如何创建我自己的样式并应用它。您可以通过更改此代码以使用 WD_STYLE_TYPE.PARAGRAPH 以相同的方式创建段落样式对象。我花了一分钟才弄清楚对象以及它们的应用级别,所以请确保您清楚地理解这一点。我发现与直觉相反的是,您在创建样式属性后定义样式属性。

这就是我创建角色级别样式对象的方法。

document = Document(path to word document)

# 创建一个字符级别样式对象(“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'

这就是我将这种风格应用到跑步中的方式。

paragraph.add_run(any string variable, style = 'CommentsStyle').bold = True

3
投票

最新版本的 python-docx 中添加了对运行样式的支持


3
投票

使用此代码会对您有很大帮助。

import docx
from docx.shared import Pt
from docx.enum.style import WD_STYLE_TYPE

doc = docx.Document()

parag = doc.add_paragraph("Hello!")

font_styles = doc.styles
font_charstyle = font_styles.add_style('CommentsStyle', WD_STYLE_TYPE.CHARACTER)
font_object = font_charstyle.font
font_object.size = Pt(20)
font_object.name = 'Times New Roman'

parag.add_run("this word document, was created using Times New Roman", style='CommentsStyle').bold = True
parag.add_run("Python", style='CommentsStyle').italic = True
doc.save("test.docx")

0
投票

python-docx 的文档在这里: http://python-docx.readthedocs.org/en/latest/

此处列出了默认模板中可用的样式: http://python-docx.readthedocs.org/en/latest/user/styles.html

在上面的示例中,您使用了字体名称(“Times New Roman”)而不是样式 ID。例如,如果您使用“Heading1”,这会改变字体的外观等,因为它是段落样式。

目前没有 API 可以直接将字体名称或字体大小应用于 python-docx 中的文本,尽管下一个版本(可能在一个月内)将提供更多功能。同时,您可以为所需的段落和字符设置定义样式并应用这些样式。使用样式是在 Word 中应用格式的推荐方法,类似于 CSS 是对 HTML 应用格式的推荐方法。


0
投票

这里是使用最新版本的 python-docx 的完整示例。这允许您创建标题、段落和页脚并独立设置字体和字体大小。

from docx import Document
from docx.shared import Pt

lorem_ipsum = """Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam euismod, nisl eget ultricies aliquam, nunc nisl aliquet nunc, quis aliquam. """

document = Document()

h1 = document.add_heading("First heading", level=1)
p1 = document.add_paragraph(lorem_ipsum)
h2 = document.add_heading("Second heading", level=1)
p2 = document.add_paragraph(lorem_ipsum)
footer_with_timestamp = document.sections[0].footer
footer_with_timestamp.paragraphs[0].text = lorem_ipsum

# Set font for all paragraphs.
for paragraph in document.paragraphs:
    for run in paragraph.runs:
        run.font.name = "Arial"

# Set font size for headings.
for paragraph in [h1, h2]:
    for run in paragraph.runs:
        run.font.size = Pt(14)

# Set font size for paragraphs.
for paragraph in [p1, p2]:
    for run in paragraph.runs:
        run.font.size = Pt(10)

# Set font and font size for footer.
for run in footer_with_timestamp.paragraphs[0].runs:
    run.font.name = "Arial"
    run.font.size = Pt(8)

document.save("demo.docx")
© www.soinside.com 2019 - 2024. All rights reserved.