How to Convert Word, Excel and PowerPoint to PDF in Python

PDF is a widely used file format that preserves document formatting across different devices and operating systems. People often convert Word, Excel and PowerPoint files to PDF for several reasons:

  • Versatility: Converting to PDF ensures that the document can be easily viewed and shared with others, regardless of the software they have installed.
  • Security: PDF files can be password protected or have limited access rights, providing an additional layer of security for sensitive information.
  • Print-friendly: PDFs are optimized for printing and provide consistent output, ensuring your document looks exactly the same when printed as it does on screen.
  • File compression: PDF files are often smaller in size than the original document, making them easier to store, share, and download.

Overall, converting Word, Excel, and PowerPoint files to PDF provides compatibility, security, and consistent formatting, making it a popular choice for sharing and archiving documents. In this article, I’ll show you how to programmatically convert these popular Office file types to PDF in Python using the Spire.Office for Python library.

  1. Convert Word Doc or Docx to PDF in Python
  2. Convert Excel XLS or XLSX to PDF in Python
  3. Convert PowerPoint PPT or PPTX to PDF in Python

Install required dependencies

This solution relies on Spire.Office for Python, which is a combination of Spire.Doc for Python, Spire.XLS for Python, Spire.Presentation for Python, and Spire.PDF for Python.

As the name suggests, they are modules for processing Word, Excel, PowerPoint and PDF documents, respectively.

Spire.Office for Python can be easily installed using the following pip command.

Convert Word Doc or Docx to PDF in Python

Spire.Doc for Python provides a Document.LoadFromFile() method that allows users to load a Doc or Docx file.

You can then convert to PDF using the Document.SaveToFile(string filename, ToPdfParameterList parameter) method.

The ToPdfParameterList object specifies transformation parameters, such as disabling hyperlinks, inserting fonts, and encrypting the resulting document.

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()

# Load a Doc or Docx file
document.LoadFromFile(“C:\\Users\\Administrator\\Desktop\\input.docx”)

# Create a ToPdfParameterList object
parameter = ToPdfParameterList()

# Disable hyperlinks in generated document
parameter.DisableLink = True

# Embed fonts in generated document
parameter.IsEmbeddedAllFonts = True

# Encrypt generated document
parameter.PdfSecurity.Encrypt(“openPsd”, “permissionPsd”, PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit)

# Save the Word document to PDF
document.SaveToFile(“output/ToPDF.pdf”, parameter)
document.Close()

Convert Excel XLS or XLSX to PDF in Python

Users can use the Workbook.LoadFromFile() method provided by Spire.XLS for Python to load an XLS or XLSX file.

Then specify the conversion settings through the properties of the Workbook.ConverterSetting object.

The entire workbook can then be converted to PDF using the Workbook.SaveToFile() method.

To convert a specific worksheet to a PDF document, use the Worksheet.SaveToPdf() method.

from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load a XLS or XLSX document
workbook.LoadFromFile(“C:\\Users\\Administrator\\Desktop\\sample.xlsx”)

# Iterate through the worksheets in the workbook
for sheet in workbook.Worksheets:

# Get the PageSetup object
pageSetup = sheet.PageSetup

# Set page margins
pageSetup.TopMargin = 0.3
pageSetup.BottomMargin = 0.3
pageSetup.LeftMargin = 0.3
pageSetup.RightMargin = 0.3

# Set worksheet to fit to page when converting
workbook.ConverterSetting.SheetFitToPage = True

# Convert to PDF file
workbook.SaveToFile(“output/ToPdf.pdf”, FileFormat.PDF)
workbook.Dispose()

Convert PowerPoint PPT or PPTX to PDF in Python

Converting from PowerPoint to PDF is also quite easy.

Simply use Presentation.LoadFromFile() method to load a PPT or PPTX file and then convert it to PDF using Presentation.SaveToFile() method.

To convert a specific slide to PDF, use the ISlide.SaveToFile() method.

from spire.presentation import *
from spire.presentation.common import *

# Create an object of Presentation class
presentation = Presentation()

# Load a PPT or PPTX file
presentation.LoadFromFile(“C:\\Users\\Administrator\\Desktop\\sample.pptx”)

# Convert the presentation file to PDF and save it
presentation.SaveToFile(“output/ToPDF.pdf”, FileFormat.PDF)
presentation.Dispose()

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *