我使用 python 2.7 和 python pptx。
我有一个数据表,需要它以一定的宽度和高度为单位,
我了解到我可以像这样更改文本大小 enter link description here
但我希望更改整个表格的大小以适应确切的位置,并更改字体大小,并操纵行高和列宽,这一切似乎都太复杂且难以处理,
我发现我可以将表格变成图像,而不是轻松地更改它的大小,但感觉这不是正确的做法。
想法?
最佳答案
示例取自 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
。
关于python pptx 判断一个表格的宽高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40626741/