{
“cells”: [
{

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“# Document conversionsn”, “n”, “In this tutorial we’ll look at some examples of converting various document formats to PDF.n”, “This step may be needed in cases when you want to addn”, “word processor documents (.doc, .docx, .odt),n”, “spreadheeets (.xls, .xlsx, .ods),n”, “or simple presentation (.ppt, .pptx, .odp).n”, “Learning Equality operates a web service called microwave that you can usen”, “to convert all kinds of documents into PDF format, which can be viewed in Kolibri.n”, “n”, “Before we begin, click [this link](http://35.185.105.222:8989/healthz) to checkn”, “that the microwave conversion service is running. You should see a message about the uptime.n”, “If you see an error get in touch with the Learning Equality content teamn”, “so we can fix it.”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Setup”

]

}, {

“cell_type”: “code”, “execution_count”: 1, “metadata”: {}, “outputs”: [], “source”: [

“import requestsn”, “n”, “# This is the main URL for the microwave servicen”, “microwave_url = "http://35.185.105.222:8989/unoconv/pdf"”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Convert a .docx file to .pdf”

]

}, {

“cell_type”: “code”, “execution_count”: 2, “metadata”: {}, “outputs”: [], “source”: [

“# helpern”, “def save_response_content(response, filename):n”, “ with open(filename, ‘wb’) as localfile:n”, “ localfile.write(response.content)n”, “n”, “# Download a sample .docx filen”, “docx_url = ‘https://calibre-ebook.com/downloads/demos/demo.docx’n”, “response1 = requests.get(docx_url)n”, “save_response_content(response1, ‘document.docx’)n”, “n”, “# Convert itn”, “microwave_url = ‘http://35.185.105.222:8989/unoconv/pdf’n”, “files = {‘file’: open(‘sample.docx’, ‘rb’)}n”, “response = requests.post(microwave_url, files=files)n”, “save_response_content(response, ‘document.pdf’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“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.”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: []

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“## Convert a .png file to .pdf”

]

}, {

“cell_type”: “code”, “execution_count”: 3, “metadata”: {}, “outputs”: [], “source”: [

“# helpern”, “def save_response_content(response, filename):n”, “ with open(filename, ‘wb’) as localfile:n”, “ localfile.write(response.content)n”, “n”, “# Let’s GET the postern”, “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’n”, “response1 = requests.get(png_url)n”, “save_response_content(response1, ‘infographic.png’)n”, “n”, “# Convert itn”, “files = {‘file’: open(‘infographic.png’, ‘rb’)}n”, “response = requests.post(microwave_url, files=files)n”, “save_response_content(response, ‘infographic.pdf’)”

]

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“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.”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: []

}, {

“cell_type”: “markdown”, “metadata”: {}, “source”: [

“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.”

]

}, {

“cell_type”: “code”, “execution_count”: null, “metadata”: {}, “outputs”: [], “source”: []

}

], “metadata”: {

“kernelspec”: {

“display_name”: “Python 3”, “language”: “python”, “name”: “python3”

}, “language_info”: {

“codemirror_mode”: {

“name”: “ipython”, “version”: 3

}, “file_extension”: “.py”, “mimetype”: “text/x-python”, “name”: “python”, “nbconvert_exporter”: “python”, “pygments_lexer”: “ipython3”, “version”: “3.7.4”

}

}, “nbformat”: 4, “nbformat_minor”: 2

}