【问题标题】:python pptx determine a table width and heightpython pptx 确定表格的宽度和高度
【发布时间】:2016-11-16 19:21:59
【问题描述】:
我使用 python 2.7 和 python pptx。
我有一个数据表,需要它来确定宽度和高度,
我知道我可以像enter link description here这样改变文字大小
但我希望更改整个表格的大小以适应确切的位置,并更改字体大小,以及操纵行高和列宽,这一切似乎都太复杂且难以处理,
我发现我可以把桌子变成一张图片,然后轻松地改变它的大小,但这感觉不是正确的做法。
想法?
【问题讨论】:
标签:
python
python-2.7
python-pptx
【解决方案1】:
取自pptx documentation 的示例提供了一个很好的示例,说明如何创建具有给定整体width 和height 的表,如下所示:
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shapes.title.text = 'Adding a Table'
rows = cols = 2
left = top = Inches(2.0)
width = Inches(6.0)
height = Inches(0.8)
table = shapes.add_table(rows, cols, left, top, width, height).table
# set column widths
table.columns[0].width = Inches(2.0)
table.columns[1].width = Inches(4.0)
# write column headings
table.cell(0, 0).text = 'Foo'
table.cell(0, 1).text = 'Bar'
# write body cells
table.cell(1, 0).text = 'Baz'
table.cell(1, 1).text = 'Qux'
prs.save('test.pptx')
util Module 还提供了指定维度的替代方法,例如 Centipoints、Cm、Emu、Mm、Pt 和 Px。