Convert Excel Spreadsheets to PDF using Python

This article will guide you to convert Excel Spreadsheets (XLS, XLSX) to PDF format in Python. Excel spreadsheets are widely used to maintain invoices, ledgers, inventory, accounts, and other reports. On the other hand, PDF is also one of the most commonly used formats and famous for its portability. Conversion among these two formats is widely required by users and programmers as well.

Convert Excel to PDF in Python

Python was developed in the 1990s and is now continuing to be one of the best and most popular languages, every developer should learn in 2020[1]. Let’s move ahead with your Excel files on Cloud storage get converted to PDF using Python.

Python SDK to Convert Documents to PDF

I will be using the Python SDK of GroupDocs.Conversion Cloud API for conversions in this article, so get your APP KEY and APP SID from the dashboard before your start following the steps and available code examples.

Convert Excel Spreadsheets to PDF in Python

Below are the simple steps to convert any Excel spreadsheet to PDF using Python:

  • Upload the Spreadsheet on the Cloud.
  • Convert the uploaded Spreadsheet.
  • Download the converted PDF document.
Convert Excel Sheet to PDF in Python - Corona Situation

Python code is shown below to give you a better idea about how simple it is:

  • Set the Convert Settings (File path and target format).
  • Set the load options using SpreadsheetLoadOptions.
  • Call the convert_document function to convert.
  • Download the converted PDF from the provided URL.
# Convert Excel Spreadsheets to PDF in Python
# Set your APP KEY and APP SID obtained from dashboard.groupdocs.cloud
apiInstance = groupdocs_conversion_cloud.ConvertApi.from_keys(Common.app_sid, Common.app_key)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "Resources/spreadsheet.xlsx"
settings.format = "pdf"
# Load Options and Output settings
loadOptions = groupdocs_conversion_cloud.SpreadsheetLoadOptions()
loadOptions.one_page_per_sheet = True
settings.load_options = loadOptions
settings.output_path = "converted"
# Convert the Spreadsheet to PDF
result = apiInstance.convert_document(groupdocs_conversion_cloud.ConvertDocumentRequest(settings))

Convert XLS, XLSX to PDF and Show Gridlines in Python

Showing spreadsheet gridlines in a PDF is not always needed but required some times. So here is a simple option that allows showing gridlines in a PDF when needed.

loadOptions = groupdocs_conversion_cloud.SpreadsheetLoadOptions()
loadOptions.show_grid_lines = True
Convert Excel Sheet to PDF Show Gridlines in Python

Convert Excel Spreadsheets to PDF with Specific Range in Python

It is not necessary to convert the whole Excel Workbook or Spreadsheet all the time. We can also convert the required portion of the spreadsheet by specifying the range in the following manner.

loadOptions = groupdocs_conversion_cloud.SpreadsheetLoadOptions()
loadOptions.convert_range = "1:35"

Customizations while Converting Spreadsheet to PDF

There are many conversion customizations while converting the spreadsheets to PDF, like:

  • Show Spreadsheet Gridlines in PDF
    • loadOptions.show_grid_lines = True
  • Hide Spreadsheet Comments in PDF
    • loadOptions.hide_comments = True
  • Skip Spreadsheet Empty Rows and Columns
    • loadOptions.skip_empty_rows_and_columns = True
  • Change Spreadsheet Font in PDF
    • loadOptions.default_font = “Helvetica” loadOptions.font_substitutes = {“Tahoma” : “Arial”, “Times New Roman” : “Arial”}
  • Convert the Specific Range of Spreadsheets to PDF
    • loadOptions.convert_range = “1:35”
  • Show Hidden Sheets of Excel in PDF
    • loadOptions.show_hidden_sheets = True

The best and easiest way to try out all the above options is to run the available examples on GitHub repository.

You can learn more about the API from the documentation or Let’s talk more @ Free Support Forum.