Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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,我正在尝试将表的内容放在中心位置。我有: 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' 它输出一个带有标题tes

我刚刚开始使用PythonDocx,我正在尝试将表的内容放在中心位置。我有:

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
可以工作,但它什么也没做


那么,如何将所有文本对齐到中心?

python docx中还不支持您询问的设置

如果您在GitHub问题列表中添加了一个问题,可能标题为:“feature:Table.alignment”,我们会将其添加到积压工作中。

如果您还没有看到它,您可以在这里找到python docx的文档:

您询问的设置在python docx中尚不受支持

如果您在GitHub问题列表中添加了一个问题,可能标题为:“feature:Table.alignment”,我们会将其添加到积压工作中。

如果您还没有看到它,您可以在这里找到python docx的文档:

以下是如何进行表格单元格垂直对齐:

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

以下是如何进行表格单元格垂直对齐:

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
在单元格中使用段落。 像这样:

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
在单元格中使用段落。 像这样:

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

谢谢,我阅读了文档,但找不到任何与表格居中有关的内容,所以我在这里询问。谢谢,我阅读了文档,但找不到任何与表格居中有关的内容,所以我在这里询问。