我刚刚开始使用python,并试图将表的内容集中起来。我有:
table = document.add_table(rows=1, cols=1)
tableHeader = table.rows[0].cells
tableHeader[0].text = 'test'
row_cells = table.add_row().cells
row_cells[0].text = 'example'
table.style = 'MediumGrid3'
它输出带有头test
和文本example
的表。
我原以为table.alignment = 1
会起作用,但什么也做不了。
那么,我如何使所有的文本与中心对齐?
发布于 2014-11-15 20:10:46
您询问的设置在python中还不支持。
如果您为它添加了一个题为"feature: Table.alignment“的问题,那么我们将把它添加到GitHub问题列表中。https://github.com/python-openxml/python-docx/issues
如果您还没有看到它,您可以在这里找到python的文档:http://python-docx.readthedocs.org/en/latest/。
发布于 2016-01-20 13:12:45
下面是如何实现表格单元格垂直对齐的方法:
import traceback
from docx.oxml.shared import OxmlElement, qn
def set_cell_vertical_alignment(cell, align="center"):
try:
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
tcValign = OxmlElement('w:vAlign')
tcValign.set(qn('w:val'), align)
tcPr.append(tcValign)
return True
except:
traceback.print_exc()
return False
发布于 2020-04-20 23:36:02
在单元格中使用段落。就像这样:
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
table = document.add_table(rows=1, cols=1)
cell = table.rows[0].cells[0]
cell_paragraph = cell.paragraphs[0]
cell_paragraph.text = 'test'
cell_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
https://stackoverflow.com/questions/26946291
复制相似问题