python pptx 获取 table 宽度
python pptx get table width
我使用 python 2.7 并使用 python pptx。
我在我的幻灯片中添加了一个 table,并且需要获得 table 总宽度。
我发现 here _column 属性宽度,并尝试使用它,例如使用该代码
for col in table._column:
yield col.width
并得到以下错误:
AttributeError: 'Table' 对象没有属性“_column”
我需要获得 table 宽度(或列宽并求和)。想法?
谢谢!
你在 Table
上想要的 属性 是 .columns
,所以:
for column in table.columns:
yield column.width
文档的 API 部分提供了所有属性和每个属性的描述,例如描述 table 对象 API 的页面:
http://python-pptx.readthedocs.io/en/latest/api/table.html
构建 Scanny 的代码和 pptx documentation 我们可以定义这样的函数来打印整个现有 python-pptx table 对象的尺寸:
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)
我使用 python 2.7 并使用 python pptx。
我在我的幻灯片中添加了一个 table,并且需要获得 table 总宽度。
我发现 here _column 属性宽度,并尝试使用它,例如使用该代码
for col in table._column:
yield col.width
并得到以下错误:
AttributeError: 'Table' 对象没有属性“_column”
我需要获得 table 宽度(或列宽并求和)。想法?
谢谢!
你在 Table
上想要的 属性 是 .columns
,所以:
for column in table.columns:
yield column.width
文档的 API 部分提供了所有属性和每个属性的描述,例如描述 table 对象 API 的页面: http://python-pptx.readthedocs.io/en/latest/api/table.html
构建 Scanny 的代码和 pptx documentation 我们可以定义这样的函数来打印整个现有 python-pptx table 对象的尺寸:
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)