簡體   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