简体   繁体   English

python pptx 判断一个表格的宽高

[英]python pptx determine a table width and height

I use python 2.7 with python pptx.我使用 python 2.7 和 python pptx。

I have a table of data and need it to feet certain width and height,我有一张数据表,需要它达到一定的宽度和高度,

I learned that i can change the text size like so enter link description here我了解到我可以像这样更改文本大小enter link description here

But i wish to change the entire table size to fit an exact location and with changing the font size, and manipulating the row heights and col width it all seems too complex and hard to handle,但我希望更改整个表格大小以适应确切位置并更改字体大小,并操纵行高和列宽,这一切似乎太复杂且难以处理,

I found that i can turn the table to an image and than easily change it's size but that does not feel like the right thing to do.我发现我可以将表格变成图像,而不是轻松地更改它的大小,但感觉这不是正确的做法。

Ideas?想法?

The example taken from the pptx documentation gives a good example on how to create a table with a given overall width and height as follows: 来自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')

The util Module also provides alternatives to specifying the dimensions such as Centipoints , Cm , Emu , Mm , Pt and Px . util模块还提供用于指定尺寸的替代方法,例如CentipointsCmEmuMmPtPx

Actually, we may try to use win32 api provided by MS Office for this issue.其实我们可以尝试使用MS Office提供的win32 api来解决这个问题。 It's very similar to vba. Hope this finds you well.它与 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

Visit Microsoft VBA Reference Document for more info.有关详细信息,请访问Microsoft VBA 参考文档

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

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