簡體   English   中英

python-pptx:如何重新定位圖片占位符?

[英]python-pptx: How canI reposition the picture placeholder?

我是python-pptx軟件包的新手,但我發現它非常有用。

我添加了帶有“標題,圖片和標題”布局的幻燈片。

SLD_LAYOUT_TITLE_AND_PICTURE_AND_CAPTION = 8

prs = Presentation(output_pptx_fp)
slide_layout = prs.slide_layouts[SLD_LAYOUT_TITLE_AND_PICTURE_AND_CAPTION]

slide = prs.slides.add_slide(slide_layout)
shapes = slide.shapes

placeholder_pict = slide.placeholders[1]  # idx key, not position

placeholder_pict.insert_picture(img_path) 

我試圖弄清楚如何重新定位圖片占位符。

通過類似於在幻燈片上放置標題占位符的方式,我嘗試使用:

placeholder_pict.left = Inches(1.96)
placeholder_pict.top = Inches(0.0)

但是,這生成了AttributeError。

我還嘗試過對“ insert_picture()”方法使用“ left”和“ top”參數:

left = Inches(1.96)
top = Inches(0.0)
placeholder_pict.insert_picture(img_path, left, top)

這生成了一個“ TypeError:insert_picture()接受2個位置參數,但給出了4個”

如何在這張幻燈片上重新放置圖片占位符? 我錯過了文檔中的某些內容嗎?

更新:

當我嘗試時(這幾乎沒什么建議,但我很好奇):

picture_left = Inches(1.96)
picture_top = Inches(0.0)
picture = placeholder_pict.insert_picture(img_path, picture_left, picture_top)

我得到:“ TypeError:insert_picture()接受2個位置參數,但給出了4個”

當我嘗試時(我認為這是斯堪尼的第一個建議):

picture_left = Inches(1.96)
picture_top = Inches(0.0)
shapes.add_picture(img_path, picture_left, picture_top)

圖片位於我想要的位置,但是它不在圖片容器(我可以忍受)中,因此它的大小不適合圖片容器(我可以通過添加代碼來處理)

當我嘗試時(我認為這是斯堪尼的第二個建議):

picture_left = Inches(0.0)
picture_top = Inches(0.0)
placeholder_pict.left = picture_left
placeholder_pict.top = picture_top
picture = placeholder_pict.insert_picture(img_path)

我沒有零錢。 圖片出現在幻燈片上,但與我完全不嘗試設置placeholder_pict.left和placeholder_pict.top的位置相同。

當我嘗試時(我認為這是斯堪尼的第三個建議):

picture = placeholder_pict.insert_picture(img_path)
picture.left = Inches(0)
picture.top = Inches(0)

我什么都沒有。 我嘗試過縮小,以查看圖片是否已“滑出”,但仍然沒有圖片的跡象。 據我所知,我什至沒有圖片容器。

當我嘗試時(按照scanny的請求):

placeholder_pict = slide.placeholders[1]  # idx key, not position
print("placeholder_pict.placeholder_format.type =", placeholder_pict.placeholder_format.type)
print("placeholder_pict.left", placeholder_pict.left, "placeholder_pict.top =", placeholder_pict.top)

我得到:
placeholder_pict.placeholder_format.type =圖片(18)
placeholder_pict.left 1792288 placeholder_pict.top = 612775

這讓我很困惑,為什么我顯然不能成功設置placeholder_pict.left和placeholder_pict.top值。

占位符的(初始)位置取決於它在其來自的幻燈片布局中的位置。 因此,如果要為使用該布局創建的所有幻燈片更改占位符的位置,則需要在布局上進行更改。

這是通過使用您自己的啟動模板PPTX,並使用PowerPoint編輯其布局以適合於手工完成的。

如果您要做的只是在特定情況下更改單個形狀的位置,則需要了解占位符是“空”形狀或形狀容器。 您需要更改由.insert_picture()調用產生的形狀的位置:

picture = placeholder_pict.insert_picture(...)
picture.left = Inches(1.96)
picture.top = Inches(0.0)

在以下位置的文檔中對此進行了描述: http : //python-pptx.readthedocs.io/en/latest/user/placeholders-using.html#insert-content-into-a-placeholder

根據其他一些觀察,這種情況似乎暴露了PowerPoint工作方式的一些“怪癖”。

簡短的答案是,如果要更改左側和頂部,則需要顯式設置寬度和高度,否則默認設置為0,這將導致形狀“消失”。

這段代碼應該可以說明這種情況:

prs = Presentation()
slide_layout = prs.slide_layouts[PICTURE_LAYOUT_IDX]
slide = prs.slides.add_slide(slide_layout)
shapes = slide.shapes

placeholder = slide.placeholders[1]
print(placeholder.left.inches, placeholder.top.inches)

# ---note that all these actions are *before* the image is inserted---

placeholder.left = Inches(3)
print(placeholder.left.inches)

print(placeholder.width)
placeholder.width = Inches(3)
print(placeholder.left.inches)

print(placeholder.height)
placeholder.height = Inches(3)
print(placeholder.height)

基本上,這表明形狀從布局占位符繼承位置和大小是“全有或全無”。 一旦您明確設置了其中之一,就需要設置所有這些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM