Example of creating a PDF using Python script

Build an automation that runs Python script from a file and generates a PDF. Python is used in automation, data generation, and conversion to various formats, including the widely used PDF.

PDFs are a portable format, making them ideal for viewing data across different devices and platforms. Python offers a diverse range of libraries for PDF generation, each with its unique features and capabilities. For example, FPDF, ReportLab, Pyppeteer, and Pdfkit.

Prerequisites

  • To run Python script from Automation 360, you must already have the latest version of Python 3.x installed on your device.
  • This example uses the FPDF library to generate the PDF. FPDF, a python library that has been ported from PHP, is a powerful tool for generating PDFs. It offers a range of functionalities, from generating PDFs from text files to writing your data formats to generate PDFs. Therefore, install it before you start building this automation by copying and pasting the following line in the Windows command prompt:
    pip install fpdf
    See FPDF project page.

Procedure

  1. Create a file in a standalone folder to hold the following Python script:
    Insert your device username in the angle brackets.
    from fpdf import FPDF
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    pdf.cell(200, 10, txt="Go Be Great!", ln=1, align="C")
    pdf.output("C:/Temp/sample_demo.pdf")
    Note: If you are uploading a script from a file on your PC, the file and any dependencies must be in a standalone folder. When you select a file for upload, all files and folders at the same folder level are uploaded.
  2. Create a new bot:
    1. On the left panel, click Automation.
    2. Click + Create > Task Bot.
    3. In the Create Task Bot window, enter the bot name.
    4. Accept the default folder location: \Bots\
      To change the default bot storage location, click Choose and follow the prompts.
    5. Click Create and edit.
  3. Provide the script with a Python Script > Open action:
    1. Double-click or drag the Python Script > Open.
    2. Select the Import existing file option.
    3. Click Browse to select the .py file you created in Step 1.
  4. Use a Python Script > Execute script action to tell the bot to run the script:
    1. Double-click or drag Python Script > Execute script.
  5. Close the script execution session with a Python Script > Close action:
    1. Double-click or drag Python Script > Close.
    2. Click Save.
  6. Click the Run icon.
    The automation creates a PDF on the specified folder with the text Go Be Great!.

    To review the bot launcher logs, navigate to C:\ProgramData\AutomationAnywhere\BotRunner\Logs\<current month>\Bot_Launcher-<today's date>.log.zip. Each zipped folder contains a file with data on the code execution, which is useful for debugging.

    If the automation does not create a file, change the last line in the Python script and make sure you have write access to the folder location specified:
    pdf.output(r"C:/Temp/sample_demo.pdf")