Read the complete article on how to add a text or image watermark to PowerPoint PPT slides in Python: https://blog.aspose.com/2022/02/09/add-watermark-to-powerpoint-ppt-in-python/
Last active
February 9, 2022 13:49
-
-
Save aspose-com-gists/5ad7480776c4d0d54a31cb34628415d8 to your computer and use it in GitHub Desktop.
Add Text or Image Watermark to PowerPoint PPT in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.slides as slides | |
import aspose.pydrawing as drawing | |
# load presentation | |
with slides.Presentation("presentation.pptx") as presentation: | |
# select slide | |
slide = presentation.slides[0] | |
# set watermark position | |
center = drawing.PointF(presentation.slide_size.size.width / 2, presentation.slide_size.size.height / 2) | |
width = 100 | |
height = 100 | |
x = center.x - width / 2 | |
y = center.y - height / 2 | |
# load image | |
with open("python-logo.png", "rb") as fs: | |
data = fs.read() | |
image = presentation.images.add_image(data) | |
# add watermark | |
watermarkShape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, x, y, height, width) | |
watermarkShape.name = "watermark" | |
# set image for watermark | |
watermarkShape.fill_format.fill_type = slides.FillType.PICTURE | |
watermarkShape.fill_format.picture_fill_format.picture.image = image | |
watermarkShape.fill_format.picture_fill_format.picture_fill_mode = slides.PictureFillMode.STRETCH | |
watermarkShape.line_format.fill_format.fill_type = slides.FillType.NO_FILL | |
# send to back | |
slide.shapes.reorder(0, watermarkShape) | |
# lock watermark to avoid modification | |
watermarkShape.shape_lock.select_locked = True | |
watermarkShape.shape_lock.size_locked = True | |
watermarkShape.shape_lock.text_locked = True | |
watermarkShape.shape_lock.position_locked = True | |
watermarkShape.shape_lock.grouping_locked = True | |
# save presentation | |
presentation.save("image-watermark-ppt.pptx", slides.export.SaveFormat.PPTX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.slides as slides | |
import aspose.pydrawing as drawing | |
# load presentation | |
with slides.Presentation("presentation.pptx") as presentation: | |
# select slide | |
slide = presentation.slides[0] | |
# set watermark position | |
center = drawing.PointF(presentation.slide_size.size.width / 2, presentation.slide_size.size.height / 2) | |
width = 300 | |
height = 300 | |
x = center.x - width / 2 | |
y = center.y - height / 2 | |
# add watermark | |
watermarkShape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, x, y, height, width) | |
watermarkShape.name = "watermark" | |
watermarkShape.fill_format.fill_type = slides.FillType.NO_FILL | |
watermarkShape.line_format.fill_format.fill_type = slides.FillType.NO_FILL | |
# set watermark text, font and color | |
watermarkTextFrame = watermarkShape.add_text_frame("Watermark") | |
watermarkPortion = watermarkTextFrame.paragraphs[0].portions[0] | |
watermarkPortion.portion_format.font_height = 52 | |
watermarkPortion.portion_format.fill_format.fill_type = slides.FillType.SOLID | |
watermarkPortion.portion_format.fill_format.solid_fill_color.color = drawing.Color.red | |
# lock watermark to avoid modification | |
watermarkShape.shape_lock.select_locked = True | |
watermarkShape.shape_lock.size_locked = True | |
watermarkShape.shape_lock.text_locked = True | |
watermarkShape.shape_lock.position_locked = True | |
watermarkShape.shape_lock.grouping_locked = True | |
# set rotation | |
watermarkShape.rotation = -45 | |
# send to back | |
slide.shapes.reorder(0, watermarkShape) | |
# save presentation | |
presentation.save("text-watermark-slide.pptx", slides.export.SaveFormat.PPTX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.slides as slides | |
import aspose.pydrawing as drawing | |
# load presentation | |
with slides.Presentation("presentation.pptx") as presentation: | |
# select slide | |
master = presentation.masters[0] | |
# set watermark position | |
center = drawing.PointF(presentation.slide_size.size.width / 2, presentation.slide_size.size.height / 2) | |
width = 300 | |
height = 300 | |
x = center.x - width / 2 | |
y = center.y - height / 2 | |
# add watermark | |
watermarkShape = master.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, x, y, height, width) | |
watermarkShape.name = "watermark" | |
watermarkShape.fill_format.fill_type = slides.FillType.NO_FILL | |
watermarkShape.line_format.fill_format.fill_type = slides.FillType.NO_FILL | |
# set watermark text, font and color | |
watermarkTextFrame = watermarkShape.add_text_frame("Watermark") | |
watermarkPortion = watermarkTextFrame.paragraphs[0].portions[0] | |
watermarkPortion.portion_format.font_height = 52 | |
watermarkPortion.portion_format.fill_format.fill_type = slides.FillType.SOLID | |
watermarkPortion.portion_format.fill_format.solid_fill_color.color = drawing.Color.red | |
# lock watermark to avoid modification | |
watermarkShape.shape_lock.select_locked = True | |
watermarkShape.shape_lock.size_locked = True | |
watermarkShape.shape_lock.text_locked = True | |
watermarkShape.shape_lock.position_locked = True | |
watermarkShape.shape_lock.grouping_locked = True | |
# send to back | |
master.shapes.reorder(0, watermarkShape) | |
# set rotation | |
watermarkShape.rotation = -45 | |
# save presentation | |
presentation.save("text-watermark-ppt.pptx", slides.export.SaveFormat.PPTX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.slides as slides | |
import aspose.pydrawing as drawing | |
# load presentation | |
with slides.Presentation("text-watermark-slide.pptx") as presentation: | |
# select slide | |
slide = presentation.slides[0] | |
shapesToRemove=[] | |
# loop through all the shapes in slide | |
for i in range(len(slide.shapes)): | |
shape = slide.shapes[i] | |
# if shape is watermark | |
if shape.name == "watermark": | |
shapesToRemove.append(shape) | |
# loop through all the shapes to be removed | |
for i in range(len(shapesToRemove)): | |
# remove shape | |
slide.shapes.remove(shapesToRemove[i]) | |
# save presentation | |
presentation.save("remove-watermark.pptx", slides.export.SaveFormat.PPTX) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment