请问pythondocx库如何将表格单元格中的文字纵向显示

img


就是使用Pythondocx库将单元格中的文字变为纵向排列

可以试试单元格字段用换行符分隔。:table.cell(0, 0).text='\n'.join(list('学生成绩'))

from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.shared import RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
import docx
document=docx.Document()
table = document.add_table(3, 3,style='Table Grid')
table.alignment = WD_TABLE_ALIGNMENT.CENTER
table.cell(0, 0).text='\n'.join(list('学生成绩'))
for column in table.columns: 
    for cell in column.cells:
        for paragraph in cell.paragraphs:  
            paragraph.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 水平对齐,居中
table.cell(0, 0).paragraphs[0].runs[0].font.color.rgb = RGBColor(255, 55, 55)  # 红色
document.save('abcd.docx')

img