Document conversions

In this tutorial we'll look at some examples of converting various document formats to PDF. This step may be needed in cases when you want to add word processor documents (.doc, .docx, .odt), spreadheeets (.xls, .xlsx, .ods), or simple presentation (.ppt, .pptx, .odp). Learning Equality operates a web service called microwave that you can use to convert all kinds of documents into PDF format, which can be viewed in Kolibri.

Before we begin, click this link to check that the microwave conversion service is running. You should see a message about the uptime. If you see an error get in touch with the Learning Equality content team so we can fix it.

Setup


In [1]:
import requests

# This is the main URL for the microwave service
microwave_url = "http://35.185.105.222:8989/unoconv/pdf"

Convert a .docx file to .pdf


In [2]:
# helper
def save_response_content(response, filename):
    with open(filename, 'wb') as localfile:
        localfile.write(response.content)

# Download a sample .docx file
docx_url = 'https://calibre-ebook.com/downloads/demos/demo.docx'
response1 = requests.get(docx_url)
save_response_content(response1, 'document.docx')

# Convert it
microwave_url = 'http://35.185.105.222:8989/unoconv/pdf'
files = {'file': open('sample.docx', 'rb')}
response = requests.post(microwave_url, files=files)
save_response_content(response, 'document.pdf')

You should now be able to see the file document.pdf in the current directory, and add this PDF file to a Kolibri channel using the DocumentFile class as part of a DocumentNode.


In [ ]:

Convert a .png file to .pdf


In [3]:
# helper
def save_response_content(response, filename):
    with open(filename, 'wb') as localfile:
        localfile.write(response.content)

# Let's GET the poster
png_url = 'https://www.who.int/images/default-source/health-topics/coronavirus/risk-communications/general-public/protect-yourself/infographics/masks-infographic---final.tmb-1920v.png'
response1 = requests.get(png_url)
save_response_content(response1, 'infographic.png')

# Convert it
files = {'file': open('infographic.png', 'rb')}
response = requests.post(microwave_url, files=files)
save_response_content(response, 'infographic.pdf')

You should now be able to see the file infographic.pdf in the current directory, and add this PDF file to a Kolibri channel using the DocumentFile class as part of a DocumentNode.


In [ ]:

The procedure works the same for spreadsheets and presentations. Just send the file to the microwave service using a POST request and you'll get a PDF version back.


In [ ]: