Python – Generate thumbnail image of PDF or Office file

July 14th, 2021 / by api2pdf /

Intro

If you are writing python code, you might be tasked with generating an image preview or thumbnail image of a PDF or Microsoft Office file like Word, Powerpoint, Excel, or even email files. If you ended up on this article you probably have been doing research for hours and getting nowhere. Maybe you ended up down a rabbit hole of looking up Libreoffice and docker containers and who knows what. Your frustration is mounting. Totally get it. Thankfully we created a REST API for you to solve this for you in minutes.

Our API will take your PDF, docx, xlsx, pptx, eml, msg, or any other file that can be reasonable opened by LibreOffice and take an image of the first page. So if you have a multi-page file, it will only grab the first page as an image. This is incredibly useful for those building galleries of image previews for files.

Generate thumbnail image of a PDF or Microsoft Office file with Python

Step 1) Open up your package manager and run the command

pip install api2pdf

Step 2) Grab an API key from https://portal.api2pdf.com. Only takes 60 seconds.

Step 3) Use the sample code below and replace “YOUR-API-KEY” with the api key you acquired in step 2.

from api2pdf import Api2Pdf

a2p_client = Api2Pdf('YOUR-API-KEY')
api_response = a2p_client.LibreOffice.thumbnail('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx')
print(api_response.result)

And that’s it! Modify the code as you see fit. Hopefully this saves you time and makes generating thumbnail images of PDFs and office files easy and painless for those writing python code.

See full github library

We have a whole python based client library for our API that does a lot more than just this. Check out the full library capabilities here: https://github.com/Api2Pdf/api2pdf.python

Tags: , , , , , , , , , , ,

Comments are closed.