Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python docx中加粗行单元格的文本?_Python_Python Docx - Fatal编程技术网

如何在python docx中加粗行单元格的文本?

如何在python docx中加粗行单元格的文本?,python,python-docx,Python,Python Docx,我正在使用pythondocx模块生成一个文档。 我想在python docx中加粗一行的特定单元格 这是密码 book_title = '\nSince you are using the docx module, you can style your text/paragraph by explicitly defining the style. In order to apply a style, use the following code snippet referenced fro

我正在使用pythondocx模块生成一个文档。 我想在python docx中加粗一行的特定单元格

这是密码

book_title = '\nSince you are using the docx module, you can style your text/paragraph by explicitly defining the style.

In order to apply a style, use the following code snippet referenced from docx documentation here.

>>> from docx import Document
>>> document = Document()
>>> style = document.styles['Normal']
>>> font = style.font
>>> font.bold= True

book_title='\n因为您使用的是docx模块,所以可以通过显式定义样式来设置文本/段落的样式

要应用样式,请使用从docx文档中引用的以下代码段


这会将应用段落的字体样式更改为粗体

以下是我对它的理解: 段落中包含的运行对象和样式粗体、斜体是运行的方法。 因此,遵循这一逻辑可以解决您的问题:

from docxtpl import DocxTemplate, RichText
doc = DocxTemplate("test.docx")
rt = RichText() #create a RichText object
rt.add('World', bold=True) #pass the text as an argument and the style, bold=True
context = { 'context_var': rt } #add context variable to the context and map it to rt
doc.render(context) #render the context
doc.save("generated_doc.docx") #save as a new document

这只是表中第一个单元格的示例。请在代码中修改它。

在python docx中,docx模板文档中任何字符的样式都可以通过使用样式来覆盖。您应该为模板中需要设置样式的特定字符/字符串在字符/字符串的位置提供上下文变量。此变量映射到具有您在代码中定义的样式定义的RichText对象,以设置字符/字符串的样式。为了使事情更清楚,请考虑一个示例模板DOC Test.docx,包含以下文本:

你好{{r context_var}}

{{..}是jinja2标记语法,{{r是重写字符样式的RichText标记。context_var是将样式映射到字符串的变量。 我们实现了如下富文本样式:

book_title = "Lord of the Rings" #or wherever you get the book title from
rt.add(book_title, bold=True)
context = { 'book_title_var': rt }
让我们看看生成的_doc.docx的内容:

你好,世界

我不确定模板是如何设计的,但如果您只希望书的标题为粗体,那么template test.docx应该有如下文本:

标题:-

{{r图书{u书名}

代码应修改为:

paragraph = row1.cells[1].paragraphs[0]
title_run = paragraph.add_run(book_title)
description_run = paragraph.add_run(book_desc)
title_run.bold = True
生成的\u doc.docx:

标题:-

指环王


单元格没有字符样式;字符样式只能应用于文本,尤其是一段文本。这实际上是一段文本的定义特征,即共享相同字符格式的字符序列,在python docx中也称为字体

若要使用与描述不同的字体获取书籍标题,它们需要在单独的运行中显示。将所有文本指定为Cell.text将导致所有文本在一次运行中显示

这可能适用于您,但假设您启动时单元格为空:

paragraph = row1.cells[1].paragraphs[0]
paragraph.add_run(book_title).bold = True
paragraph.add_run(book_desc)
此代码可以更加紧凑:


但是,也许前一个版本更清楚地说明了您在每个步骤中执行的操作。

我必须为行单元格而不是段落设置样式。我已经为段落设置了样式,需要为行单元格设置样式。
paragraph = row1.cells[1].paragraphs[0]
paragraph.add_run(book_title).bold = True
paragraph.add_run(book_desc)