I have a document in docx format generated from a pptx containing slides. I want to resize the slides in the pages of the docx by automation using python libraries. I tried with python-docx and aspose lib but no great results.
I want to be able to resize by code the OLE objects referring to imported PPTX slides into my docx document. Is someone knowing how to properly manipulate OLE objects properties to modify size into a docx document ?
from docx import Document
from docx.shared import Cm
from docx.enum.section import WD_ORIENT
# Open existing file
doc = Document('votre_document_simple.docx')
# Landscape format
section = doc.sections
section.page_height = 8.5
section.page_width = 11
section = doc.sections[-1]
section.orientation = WD_ORIENT.LANDSCAPE
new_widht, new_height = section.page_height, section.page_width
section.page_width = new_widht
section.page_height = new_height
# Margin
section.top_margin = Cm(3)
section.bottom_margin = Cm(2.54)
section.left_margin = Cm(2.22)
section.right_margin = Cm(1.59)
# Suppress "Diapositive X" strings from pages
for para in doc.paragraphs:
if "Diapositive" in para.text:
p = para._element
p.getparent().remove(p)
p._element = p._p = None
# Enumerate OLE objects to resize slides images
for rel in doc.part.rels.values():
if "media/image" in rel.target_ref:
print(f"OLE object founded : {rel.target_ref}")
# Retrieve OLE object to resize it
for shape in doc.inline_shapes:
if shape._inline.graphic.graphicData.uri.endswith(rel.target_ref):
# Redimensionner l'objet OLE
shape.width = Cm(21.32) # Ajuster la largeur
shape.height = Cm(16) # Ajuster la hauteur
#doc.save("test5.docx")