Skip to content

Instantly share code, notes, and snippets.

@kernoeb
Last active August 19, 2020 12:25
Show Gist options
  • Save kernoeb/73412c180611b583a2f1aef7583a1fbd to your computer and use it in GitHub Desktop.
Save kernoeb/73412c180611b583a2f1aef7583a1fbd to your computer and use it in GitHub Desktop.
pdf2odp : convert pdf files to LibreOffice Impress odp documents (pictures)
# Author: kernoeb
# pdf2odp.py
###########################
# Please install : #
# pip3 install odfpy #
# pip3 install pdf2image #
###########################
import os
import shutil
import sys
from odf.draw import Page, Frame, Image
from odf.opendocument import OpenDocumentPresentation
from odf.style import Style, DrawingPageProperties, MasterPage, PageLayout, PageLayoutProperties
from pdf2image import convert_from_path
pathpdf = './imagespdf/'
sizeX = '1920pt'
sizeY = '1080pt'
if len(sys.argv) > 1:
print(sys.argv[1])
try:
os.mkdir(pathpdf, 0o755)
except FileExistsError:
pass
pages = convert_from_path(sys.argv[1], 500)
countpage = 0
for page in pages:
page.save(pathpdf + 'page_' + str(countpage) + '.jpg', 'JPEG')
countpage += 1
doc = OpenDocumentPresentation()
pagelayout = PageLayout(name="Diapo")
doc.automaticstyles.addElement(pagelayout)
pagelayout.addElement(PageLayoutProperties(margin="0pt", pagewidth=sizeX,
pageheight=sizeY, printorientation="landscape"))
dpstyle = Style(name="dp1", family="drawing-page")
dpstyle.addElement(DrawingPageProperties(transitiontype="automatic", # You can edit transition here
transitionstyle="move-from-top", duration="PT1S"))
doc.automaticstyles.addElement(dpstyle)
for count in range(countpage):
masterpage = MasterPage(name=str(count), pagelayoutname=pagelayout)
doc.masterstyles.addElement(masterpage)
page = Page(stylename=dpstyle, masterpagename=masterpage)
doc.presentation.addElement(page)
photoframe = Frame(width=sizeX, height=sizeY, x="0pt", y="0pt")
page.addElement(photoframe)
href = doc.addPicture(pathpdf + 'page_' + str(count) + '.jpg')
photoframe.addElement(Image(href=href))
doc.save("Diapo.odp")
shutil.rmtree(pathpdf)
else:
print("Veuillez mettre le pdf en argument!")
@selimgr
Copy link

selimgr commented Jun 23, 2020

nice! very useful, thanks

@bizouarn
Copy link

On Windows you just have to install "poppler".
as indicated on this link https://pypi.org/project/pdf2image/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment