gpt4 book ai didi

python-2.7 - Python-docx - 如何更改表格字体大小?

转载 作者:行者123 更新时间:2023-12-04 12:13:49 24 4
gpt4 key购买 nike

table = document.add_table(rows=1, cols=1)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'

我必须用一行和一列更改表格中文本“数量”的字体大小,我该怎么做?

最佳答案

您需要获取单元格中的段落。来自 python-docx 的文档:

3.5.2 _Cell objects:
class docx.table._Cell (tc, parent)

paragraphs
List of paragraphs in the cell. A table cell is required to contain at least one block-level element and end with a paragraph. By default, a new cell contains a single paragraph. Read-only



引用: python-docx Documentation - Read the Docs

代码:

更改文本“数量”的字体大小
paragraph =hdr_cells[0].paragraphs[0]
run = paragraph.runs
font = run[0].font
font.size= Pt(30) # font size = 30

要更改整个表格的字体大小:
for row in table.rows:
for cell in row.cells:
paragraphs = cell.paragraphs
for paragraph in paragraphs:
for run in paragraph.runs:
font = run.font
font.size= Pt(30)

如何访问表格中的段落引用: Extracting data from tables

关于python-2.7 - Python-docx - 如何更改表格字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43007725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com