如何使用python-docx将表格中单元格的文本对齐到居中?

时间:2019-08-28 05:39:44

标签: python-docx

我搜索了很多,但无法通过。如果有人有python-docx的经验,可以帮助解决以下问题。

我的代码如下:

from docx import Document
from docx.shared import Cm, Inches
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.enum.table import WD_ALIGN_VERTICAL
from docx.enum.section import WD_ORIENT
from docx.enum.style import WD_STYLE_TYPE
from docx.enum.text import WD_ALIGN_PARAGRAPH

chart = Document()

style = chart.styles['Normal']
font = style.font

from docx.shared import Pt
font.size = Pt(12)
font.underline = False

# Table Starts
print("Table Making Starts..")
table = chart.add_table(rows=1, cols=2, style='Table Grid')

# Adding Table Heading Text
cell0 = table.cell(0, 0)
cell0.vertical_alignment = WD_ALIGN_VERTICAL.CENTER
cell0.bold = True
cell0.text = "Column1"

cell1 = table.cell(0, 1)
cell1.bold = True
cell1.text = "Column2"
cell1.vertical_alignment = WD_ALIGN_VERTICAL.CENTER

chart.save('./test7.docx')

但是问题是它没有进行格式化以使文本居中对齐。

我的代码输出:

enter image description here

预期输出: enter image description here

0 个答案:

没有答案