0

我使用 python 2.7 并使用 python pptx。

我在幻灯片中添加了一个表格,并且需要获取表格的整体宽度。

我在这里找到了 _column 属性宽度,并尝试使用它,例如使用该代码

for col in table._column:
    yield col.width

并得到以下错误:

AttributeError:“表”对象没有属性“_column”

我需要获取表格宽度(或列宽度并求和)。想法?

谢谢!

4

2 回答 2

1

你想要的属性Table.columns,所以:

for column in table.columns:
    yield column.width

所有属性和每个属性的描述都可以在文档的 API 部分中找到,例如描述表对象 API 的页面:http: //python-pptx.readthedocs.io/en/latest/api/table.html

于 2016-11-07T20:28:09.763 回答
0

在 Scanny 的代码和pptx 文档的基础上,我们可以定义一个这样的函数来打印整个现有 python-pptx 表对象的尺寸:

from pptx import Presentation
from pptx.util import Inches, Cm, Pt

def table_dims(table, measure = 'Inches'):
    """
    Returns a dimensions tuple (width, height) of your pptx table 
    object in Inches, Cm, or Pt. 
    Defaults to Inches.
    This value can then be piped into an Inches, Cm, or Pt call to 
    generate a new table of the same initial size. 
    """

    widths = []
    heights = []

    for column in table.columns:
        widths.append(column.width)
    for row in table.rows:
        heights.append(row.height)

    # Because the initial widths/heights are stored in a strange format, we'll convert them
    if measure == 'Inches':
        total_width = (sum(widths)/Inches(1)) 
        total_height = (sum(heights)/Inches(1))
        dims = (total_width, total_height)
        return dims

    elif measure == 'Cm':
        total_width = (sum(widths)/Cm(1))
        total_height = (sum(heights)/Cm(1))
        dims = (total_width, total_height)
        return dims

    elif measure == 'Pt':
        total_width = (sum(widths)/Pt(1))
        total_height = (sum(heights)/Pt(1))
        dims = (total_width, total_height)
        return dims

    else:
        Exception('Invalid Measure Argument')

# Initialize the Presentation and Slides objects
prs = Presentation('path_to_existing.pptx')
slides = prs.slides

# Access a given slide's Shape Tree
shape_tree = slides['replace w/ the given slide index'].shapes

# Access a given table          
table = shape_tree['replace w/ graphic frame index'].table

# Call our function defined above
slide_table_dims = table_dims(table)
print(slide_table_dims)
于 2019-01-29T18:29:00.040 回答