繁体   English   中英

python pptx 判断一个表格的宽高

[英]python pptx determine a table width and height

我使用 python 2.7 和 python pptx。

我有一张数据表,需要它达到一定的宽度和高度,

我了解到我可以像这样更改文本大小enter link description here

但我希望更改整个表格大小以适应确切位置并更改字体大小,并操纵行高和列宽,这一切似乎太复杂且难以处理,

我发现我可以将表格变成图像,而不是轻松地更改它的大小,但感觉这不是正确的做法。

想法?

来自pptx文档的示例为如何创建具有给定整体widthheight的表提供了一个很好的示例,如下所示:

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模块还提供用于指定尺寸的替代方法,例如CentipointsCmEmuMmPtPx

其实我们可以尝试使用MS Office提供的win32 api来解决这个问题。 它与 vba 非常相似。希望这能找到您。

import win32com.client as win32
from win32com.client import constants
# 使用EnsureDispatch方法创建PowerPoint应用程序对象
#Use EnsureDispatch Method
ppt = win32.gencache.EnsureDispatch('PowerPoint.Application')
# iterate through all tables in the active document
for slide in ppt.ActivePresentation.Slides:
    for shape in slide.Shapes:
        # find out if there's a table in the slide
        if shape.HasTable == -1:
            table = shape.Table
            # Set the table width to 24cm, We can divide the number of centimetres by 0.035278 to get the number of points.
            shape.Width = 24 / 0.035278
            # Set the table height to 24cm
            shape.Height = 24 / 0.035278
            # Set the height of the first row of the table to 1cm
            table.Rows(1).Height = 1 / 0.035278

有关详细信息,请访问Microsoft VBA 参考文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM