Python (359)
Convert PDF to Images in Python (PNG, JPG, BMP, SVG, TIFF)
2023-09-18 01:16:01 Written by Administrator
Converting PDF files to images in Python is a common need for developers and professionals working with digital documents. Whether you want to generate thumbnails, create previews, extract specific content areas, or prepare files for printing, transforming a PDF into image formats gives you flexibility and compatibility across platforms.
This comprehensive guide demonstrates how to convert PDF files into popular image formats—such as PNG, JPG, BMP, SVG, and TIFF—in Python, using practical, easy-to-follow code examples.
Table of Contents
- Why Convert PDF to Image
- Python PDF-to-Image Converter Library
- Simple PDF to PNG, JPG, and BMP Conversion
- Advanced Conversion Options
- Generate Multi-Page TIFF from PDF
- Export PDF as SVG
- Conclusion
- FAQs
Why Convert PDF to Image?
Converting PDF to image formats offers several benefits:
- Cross-platform compatibility: Images are easier to embed in web pages, mobile apps, or presentations.
- Preview and thumbnail generation: Quickly create page snapshots without rendering the full PDF.
- Selective content extraction: Save specific areas of a PDF as images for focused analysis or reuse.
- Simplified sharing: Images can be easily emailed, uploaded, or displayed without special PDF readers.
Python PDF-to-Image Converter Library
Spire.PDF for Python is a powerful and easy-to-use library designed for handling PDF files. It enables developers to convert PDF pages into multiple image formats like PNG, JPG, BMP, SVG, and TIFF with excellent quality and performance.

Installation
You can easily install the library using pip. Simply open your terminal and run the following command:
pip install Spire.PDF
Simple PDF to PNG, JPG, and BMP Conversion
The SaveAsImage method of the PdfDocument class allows you to render each page of a PDF into an image format of your choice.
The code example below demonstrates how to load a PDF file, iterate through its pages, and save each one as a PNG image. You can easily adjust the file format to JPG or BMP by changing the file extension.
from spire.pdf import *
# Load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("template.pdf")
# Loop through pages and save as images
for i in range(pdf.Pages.Count):
# Convert each page to image
with pdf.SaveAsImage(i) as image:
# Save in different formats as needed
image.Save(f"Output/ToImage_{i}.png")
# image.Save(f"Output/ToImage_{i}.jpg")
# image.Save(f"Output/ToImage_{i}.bmp")
# Close the PDF document
pdf.Close()

Advanced Conversion Options
Enable Transparent Image Background
Transparent backgrounds help integrate images seamlessly into designs, avoiding unwanted borders or background colors.
To enable a transparent background during PDF-to-image conversion in Python, use the SetPdfToImageOptions() method with an alpha value of 0. This setting ensures that the background of the output image is fully transparent.
The following example demonstrates how to export each PDF page as a transparent PNG image.
from spire.pdf import *
# Load PDF document from file
pdf = PdfDocument()
pdf.LoadFromFile("template.pdf")
# Set the transparent value of the image's background to 0
pdf.ConvertOptions.SetPdfToImageOptions(0)
# Loop through all pages and save each as an image
for i in range(pdf.Pages.Count):
# Convert each page to an image
with pdf.SaveAsImage(i) as image:
# Save the image to the output directory
image.Save(f"Output/ToImage_{i}_transparent.png")
# Close the PDF document
pdf.Close()
Note: Transparency is supported in PNG but not in JPG or BMP formats.
Crop Specific PDF Areas to Image
In some cases, you may only need to export a specific area of a PDF page—such as a chart, table, or block of text. This can be done by adjusting the page’s CropBox before rendering.
The CropBox property defines the visible region of the page used for display and printing. By setting it to a specific RectangleF(x, y, width, height) value, you can isolate and export only the desired portion of the content.
The example below demonstrates how to crop a rectangular area on the first page of a PDF and save that section as a PNG image.
from spire.pdf import *
# Load the PDF document from file
pdf = PdfDocument()
pdf.LoadFromFile("Sample.pdf")
# Access the first page of the PDF
page = doc.Pages.get_Item(0)
# Define the crop area of the page using a rectangle (x, y, width, height)
page.CropBox = RectangleF(0.0, 300.0, 600.0, 260.0)
# Convert the cropped page to an image
with pdf.SaveAsImage(0) as image:
# Save the image to a PNG file
image.Save("Output/CropPDFSaveAsImage.png")
# Close the PDF document
pdf.Close()
Note: You need to adjust the coordinates based on the location of your target content. Coordinates start from the top-left corner of the page.

Generate Multi-Page TIFF from PDF
The TIFF format supports multi-page documents, making it a popular choice for archival and printing purposes. Although Spire.PDF for Python doesn't natively create multi-page TIFFs, you can render individual pages as images and then use the Pillow library to merge them into one .tiff file.
Before proceeding, ensure Pillow is installed by running:
pip install Pillow
The following example illustrates how to:
- Load a PDF
- Convert each page to an image
- Combine all images into a single multi-page TIFF
from spire.pdf import *
from PIL import Image
from io import BytesIO
# Load the PDF document from file
pdf = PdfDocument()
pdf.LoadFromFile("Input.pdf")
# Create an empty list to store PIL Images
images = []
# Iterate through all pages in the document
for i in range(pdf.Pages.Count):
# Convert a specific page to an image stream
with pdf.SaveAsImage(i) as imageData:
# Open the image stream as a PIL image
img = Image.open(BytesIO(imageData.ToArray()))
# Append the PIL image to list
images.append(img)
# Save the PIL Images as a multi-page TIFF file
images[0].save("Output/ToTIFF.tiff", save_all=True, append_images=images[1:])
# Dispose resources
pdf.Dispose()

It’s also possible to convert TIFF files back to PDF. For detailed instructions on it, please refer to the tutorial: Python: Convert PDF to TIFF and TIFF to PDF.
Export PDF as SVG
SVG (Scalable Vector Graphics) is an ideal format for content that requires scaling without quality loss, such as charts, vector illustrations, and technical diagrams.
By using the SaveToFile() method with the FileFormat.SVG option, you can export PDF pages as SVG files. This conversion preserves the vector characteristics of the content, making it well-suited for web embedding, responsive design, and further editing in vector graphic tools.
The following example demonstrates how to export an entire PDF document to SVG format.
from spire.pdf import *
# Load the PDF document from file
pdf = PdfDocument()
pdf.LoadFromFile("Example.pdf")
# Save each page of the file to a separate SVG file
pdf.SaveToFile("PdfToSVG/ToSVG.svg", FileFormat.SVG)
# Close the PdfDocument object
pdf.Close()
Note: Each page in the PDF will be saved as a separate SVG file named ToSVG_i.svg, where i is the page number (1-based).
To export specific pages or customize the SVG output size, please refer to our detailed guide: Python: Convert PDF to SVG.
Conclusion
Converting PDF to images in formats like PNG, JPG, BMP, SVG, and TIFF provides flexibility for sharing, displaying, and processing digital documents. With Spire.PDF for Python, you can:
- Export high-quality images from PDFs in various formats
- Crop specific regions for focused content extraction
- Generate multi-page TIFF files for archival purposes
- Create scalable SVG vector graphics for diagrams and charts
By automating PDF to image conversion in Python, you can seamlessly integrate image export into your applications and workflows.
FAQs
Q1: Can I convert a range of pages from a PDF to images?
A1: Yes. You can convert specific pages by specifying their indices in a loop. For example, to export pages 1 to 3:
# Convert only pages 1-3
for i in range(0, 3): # 0-based index
with pdf.SaveAsImage(i) as img:
img.Save(f"page_{i}.png")
Q2: Can I batch convert multiple PDF files to images?
A2: Yes, batch conversion is supported. You can iterate through a list of PDF file paths and convert each one within a loop.
pdf_files = ["a.pdf", "b.pdf", "c.pdf"]
for file in pdf_files:
pdf = PdfDocument()
pdf.LoadFromFile(file)
for i in range(pdf.Pages.Count):
with pdf.SaveAsImage(i) as img:
img.Save(f"{file}_page_{i}.png")
Q3: Is it possible to convert password-protected PDFs to images?
A3: Yes, you can convert secured PDFs to images as long as you provide the correct password when loading the PDF document.
pdf = PdfDocument()
pdf.LoadFromFile("protected.pdf", "password")
Q4: Is it possible to extract embedded images from a PDF instead of rendering pages?
A4: Yes. Aside from rendering entire pages, the library also supports extracting images directly from the PDF.
Get a Free License
To fully experience the capabilities of Spire.PDF for Python without any evaluation limitations, you can request a free 30-day trial license.
Text wrapping and unwrapping are powerful formatting options in Microsoft Excel that offer flexibility in displaying text within cells. When text wrapping is enabled, long text is automatically wrapped into multiple lines within a cell, which ensures that the entire content is visible without truncation. This feature is particularly useful for presenting lengthy descriptions, notes, or paragraphs within a confined cell space. On the other hand, text unwrapping allows you to remove line breaks and display the text in a single line within the cell. This can be beneficial in scenarios where you need to fit the text into a specific layout or when exporting data to other applications or file formats that may not handle wrapped text correctly. In this article, we will demonstrate how to wrap or unwrap text in Excel cells in Python using Spire.XLS for Python.
Install Spire.XLS for Python
This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.XLS
If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows
Wrap or Unwrap Text in Excel Cells in Python
Spire.XLS for Python provides the CellStyle.WrapText property to control whether the text should be wrapped or unwrapped within a cell. If you want to wrap text in a cell, you can set the property as True. Conversely, if you want to unwrap text in a cell, you can set the property as False.
The following steps explain how to wrap or unwrap text in an Excel cell using Spire.XLS for Python:
- Create a Workbook object.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worksheets[] property.
- Get a specified cell using Worksheet.Range[] property.
- Get the style of the specified cell using CellRange.Style property.
- Wrap the text in the cell by setting the CellStyle.WrapText property to True. Or unwrapping the text in the cell by setting the CellStyle.WrapText property to False.
- Save the resulting file using Workbook.SaveToFile() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load a sample Excel file
workbook.LoadFromFile("Sample.xlsx")
# Get the first worksheet of the file
sheet = workbook.Worksheets[0]
# Wrap the text in cell B3
sheet.Range["B3"].Style.WrapText = True
# Unwrap the text in cell B7
sheet.Range["B7"].Style.WrapText = False
#Save the resulting file
workbook.SaveToFile("WrapOrUnwrapTextInCells.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Adding or removing rows and columns in a Word table allows you to adjust the table's structure to accommodate your data effectively. By adding rows and columns, you can effortlessly expand the table as your data grows, ensuring that all relevant information is captured and displayed in a comprehensive manner. On the other hand, removing unnecessary rows and columns allows you to streamline the table, eliminating any redundant or extraneous data that may clutter the document. In this article, we will demonstrate how to add or delete table rows and columns in Word in Python using Spire.Doc for Python.
- Add or Insert a Row into a Word Table in Python
- Add or Insert a Column into a Word Table in Python
- Delete a Row from a Word Table in Python
- Delete a Column from a Word Table in Python
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Add or Insert a Row into a Word Table in Python
You can add a row to the end of a Word table or insert a row at a specific location of a Word table using the Table.AddRow() or Table.InsertRow() method. The following are the detailed steps:
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Get the first section of the document using Document.Sections[] property.
- Get the first table of the section using Section.Tables[] property.
- Insert a row at a specific location of the table using Table.Rows.Insert() method.
- Add data to the newly inserted row.
- Add a row to the end of the table using Table.AddRow() method.
- Add data to the newly added row.
- Save the resulting document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Table1.docx")
# Get the first section of the document
section = document.Sections.get_Item(0)
# Get the first table of the first section
table = section.Tables.get_Item(0) if isinstance(section.Tables.get_Item(0), Table) else None
# Insert a row into the table as the third row
table.Rows.Insert(2, table.AddRow())
# Get the inserted row
insertedRow = table.Rows[2]
# Add data to the row
for i in range(insertedRow.Cells.Count):
cell = insertedRow.Cells[i]
paragraph = cell.AddParagraph()
paragraph.AppendText("Inserted Row")
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
# Add a row at the end of the table
addedRow = table.AddRow()
# Add data to the row
for i in range(addedRow.Cells.Count):
cell = addedRow.Cells[i]
paragraph = cell.AddParagraph()
paragraph.AppendText("End Row")
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
# Save the resulting document
document.SaveToFile("AddRows.docx", FileFormat.Docx2016)
document.Close()

Add or Insert a Column into a Word Table in Python
Spire.Doc for Python doesn't offer a direct method to add or insert a column into a Word table. But you can achieve this by adding or inserting cells at a specific location of each table row using TableRow.Cells.Add() or TableRow.Cells.Insert() method. The detailed steps are as follows:
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Get the first section of the document using Document.Sections[] property.
- Get the first table of the section using Section.Tables[] property.
- Loop through each row of the table.
- Create a TableCell object, then insert it at a specific location of each row using TableRow.Cells.Insert() method and set cell width.
- Add data to the cell and set text alignment.
- Add a cell to the end of each row using TableRow.AddCell() method and set cell width.
- Add data to the cell and set text alignment.
- Save the resulting document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Table1.docx")
# Get the first section of the document
section = document.Sections.get_Item(0)
# Get the first table of the first section
table = section.Tables.get_Item(0) if isinstance(section.Tables.get_Item(0), Table) else None
# Loop through the rows of the table
for i in range(table.Rows.Count):
row = table.Rows.get_Item(i)
# Create a TableCell object
cell = TableCell(document)
# Insert the cell as the third cell of the row and set cell width
row.Cells.Insert(2, cell)
cell.Width = row.Cells[0].Width
# Add data to the cell
paragraph = cell.AddParagraph()
paragraph.AppendText("Inserted Column")
# Set text alignment
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
# Add a cell to the end of the row and set cell width
cell = row.AddCell()
cell.Width = row.Cells[1].Width
# Add data to the cell
paragraph = cell.AddParagraph()
paragraph.AppendText("End Column")
# Set text alignment
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
# Save the resulting document
document.SaveToFile("AddColumns.docx", FileFormat.Docx2016)
document.Close()

Delete a Row from a Word Table in Python
To delete a specific row from a Word table, you can use the Table.Rows.RemoveAt() method. The detailed steps are as follows:
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Get the first section of the document using Document.Sections[] property.
- Get the first table of the section using Section.Tables[] property.
- Remove a specific row from the table using Table.Rows.RemoveAt() method.
- Save the resulting document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("AddRows.docx")
# Get the first section of the document
section = document.Sections.get_Item(0)
# Get the first table of the first section
table = section.Tables.get_Item(0) if isinstance(section.Tables.get_Item(0), Table) else None
# Remove the third row
table.Rows.RemoveAt(2)
# Remove the last row
table.Rows.RemoveAt(table.Rows.Count - 1)
# Save the resulting document
document.SaveToFile("RemoveRows.docx", FileFormat.Docx2016)
document.Close()

Delete a Column from a Word Table in Python
To delete a specific column from a Word table, you need to remove the corresponding cell from each table row using the TableRow.Cells.RemoveAt() method. The detailed steps are as follows:
- Create a Document object.
- Load a Word document using Document.LoadFromFile() method.
- Get the first section of the document using Document.Sections[] property.
- Get the first table of the section using Section.Tables[] property.
- Loop through each row of the table.
- Remove a specific cell from each row using TableRow.Cells.RemoveAt() method.
- Save the resulting document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("AddColumns.docx")
# Get the first section of the document
section = document.Sections.get_Item(0)
# Get the first table of the first section
table = section.Tables.get_Item(0) if isinstance(section.Tables.get_Item(0), Table) else None
# Loop through the rows of the table
for i in range(table.Rows.Count):
row = table.Rows.get_Item(i)
# Remove the third cell from the row
row.Cells.RemoveAt(2)
# Remove the last cell from the row
row.Cells.RemoveAt(row.Cells.Count - 1)
# Save the resulting document
document.SaveToFile("RemoveColumns.docx", FileFormat.Docx2016)
document.Close()

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
The Excel workbook is a powerful spreadsheet that enables the creation, manipulation, and analysis of data in a variety of ways. One of the useful features that workbooks offer is the ability to hide or unhide worksheets in a workbook. Hiding worksheets can help protect sensitive or confidential information, reduce clutter, or organize data more efficiently. And when users need to re-display the hidden worksheets, they can also unhide them with simple operations. This article is going to explain how to hide or unhide worksheets in Excel workbooks through Python programs using Sprie.XLS for Python.
Install Spire.XLS for Python
This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.XLS
If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows
Hide Excel Worksheets in Python
The Worksheet.Visibility property in Spire.XLS for Python can be used to set the visibility of a worksheet. By assigning WorksheetVisibility.Hidden or WorksheetVisibility.StrongHidden to this property, users can change the visibility of a worksheet to hidden or very hidden (completely not shown in Excel and can only be unhidden through code).
The detailed steps for hiding worksheets are as follows:
- Create an object of Workbook class.
- Load a workbook using Workbook.LoadFromFile() method.
- Change the status of the first worksheet to hidden by assigning WorksheetVisibility.Hidden to the Workbook.Worksheets[].Visibility property.
- Change the status of the second worksheet to very hidden by assigning WorksheetVisibility.StrongHidden to the Workbook.Worksheets[].Visibility property.
- Save the workbook using Workbook.SaveToFile() method.
- Python
from spire.xls import *
# Create an object of Workbook
workbook = Workbook()
# Load an Excel workbook
workbook.LoadFromFile("Sample.xlsx")
# Hide the first worksheet
workbook.Worksheets[0].Visibility = WorksheetVisibility.Hidden
# Change the second worksheet to very hidden
workbook.Worksheets[1].Visibility = WorksheetVisibility.StrongHidden
# Save the workbook
workbook.SaveToFile("output/HideWorksheets.xlsx")

Unhide Excel Worksheets in Python
Unhiding a worksheet can be done by assigning WorksheetVisibility.Visible to the Workbook.Worksheets[].Visibility property. The detailed steps are as follows:
- Create an object of Workbook class.
- Load a workbook using Workbook.LoadFromFile() method.
- Unhide the very hidden worksheet by assigning WorksheetVisibility.Visible to the Workbook.Worksheets[].Visibility property.
- Save the workbook using Workbook.SaveToFile() method.
- Python
from spire.xls import *
# Create an object of Workbook
workbook = Workbook()
# Load an Excel workbook
workbook.LoadFromFile("output/HideWorksheets.xlsx")
# Unhide the second worksheet
workbook.Worksheets[0].Visibility = WorksheetVisibility.Visible
# Save the workbook
workbook.SaveToFile("output/UnhideWorksheet.xlsx")

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Converting a Word document into images can be a useful and convenient option when you want to share or present the content without worrying about formatting issues or compatibility across devices. By converting a Word document into images, you can ensure that the text, images, and formatting remain intact, making it an ideal solution for sharing documents on social media, websites, or through email. In this article, you will learn how to convert Word to PNG, JPEG or SVG in Python using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Convert Word to PNG or JPEG in Python
Spire.Doc for Python offers the Document.SaveImageToStream() method to convert a certain page into a bitmap image. Afterwards, you can save the bitmap image to a popular image format such as PNG, JPEG, or BMP. The detailed steps are as follows.
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Retrieve each page in the document, and convert a specific page into a bitmap image using Document.SaveImageToStreams() method.
- Save the bitmap image into a PNG or JPEG file.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word file
document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx")
# Loop through the pages in the document
for i in range(document.GetPageCount()):
# Convert a specific page to bitmap image
imageStream = document.SaveImageToStreams(i, ImageType.Bitmap)
# Save the bitmap to a PNG file
with open('Output/ToImage-{0}.png'.format(i),'wb') as imageFile:
imageFile.write(imageStream.ToArray())
document.Close()

Convert Word to SVG in Python
To convert a Word document into multiple SVG files, you can simply use the Document.SaveToFile() method. Here are the steps.
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Convert it to individual SVG files using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word file
document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx")
# Convert it to SVG files
document.SaveToFile("output/ToSVG.svg", FileFormat.SVG)
document.Close()

Get a Free License
To fully experience the capabilities of Spire.Doc for Python without any evaluation limitations, you can request a free 30-day trial license.
Watermarks in Word documents serve as overlayed text or pictures that are typically used to indicate documents’ status, confidentiality, draft nature, etc. While they are useful in certain contexts, watermarks often become a hindrance when it comes to presenting documents. They can be distracting, obscuring the readability, and reduce the overall quality of the document. This article will show how to remove watermarks from Word documents in Python programs using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Remove the Watermark from a Word Document
Spire.Doc for Python provides the Document.Watermark property which allows users to deal with the watermark of a Word document. Users can assign a null value to this property to remove the watermark of Word document. The detailed steps are as follows:
- Create an object of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Remove the watermark by assigning a null value to Document.Watermark property.
- Save the document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an object of Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Sample.docx")
# Remove the watermark
doc.Watermark = None
# Save the document
doc.SaveToFile("output/RemoveWatermark.docx", FileFormat.Auto)

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Convert PowerPoint PPT or PPTX to PDF in Python: A Comprehensive Guide
2023-09-12 01:08:24 Written by Koohji
Looking to convert PowerPoint PPT or PPTX files to PDF using Python? This comprehensive guide walks you through the process of converting PowerPoint to PDF with ease. You'll learn how to perform quick conversions using default settings, as well as explore advanced features such as exporting specific slides, adjusting slide size for optimal output, including hidden slides, and generating PDF/A-compliant files for archival use.
- Why Convert PowerPoint to PDF
- Python PowerPoint to PDF Converter Library Installation
- Convert PowerPoint to PDF with Default Settings
- Export PowerPoint to PDF with Advanced Settings
- Conclusion
- FAQs
Why Convert PowerPoint to PDF?
Converting PowerPoint files to PDF offers several advantages:
- Universal Compatibility: PDF files can be opened on virtually any device or operating system without needing PowerPoint installed.
- Preserved Formatting: Unlike PowerPoint files, PDFs lock in layout, fonts, and images to avoid rendering inconsistencies.
- Enhanced Security: PDF files can be encrypted, making them ideal for sharing confidential information.
- Reduced File Size: PDFs often have a smaller file size than PowerPoint presentations, making them easier to share via email or upload online.
Python PowerPoint to PDF Converter Library Installation
To convert PowerPoint presentations to PDF in Python, you can use Spire.Presentation for Python. This powerful library allows you to create, read, modify, and convert PowerPoint PPTX and PPT files without needing Microsoft PowerPoint installed.
Why Choose Spire.Presentation for Python?
- High Fidelity: Ensures accurate conversion while preserving formatting and layout.
- User-Friendly: Simple API makes it easy to implement in your projects.
- Versatile: Supports a wide range of PowerPoint features and formats.
Install Spire.Presentation for Python
Before starting with the conversion process, install Spire.Presentation via pip using the following command:
pip install Spire.Presentation
Need help with installation? Refer to this detailed documentation: How to Install Spire.Presentation for Python on Windows
Convert PowerPoint to PDF with Default Settings
Spire.Presentation makes it easy to convert PowerPoint files to PDF with just a few lines of code.
The example below shows how to load a .pptx or .ppt file and export it to PDF using default settings - ideal for quick conversions where no customization is needed.
from spire.presentation import *
# Create a Presentation object
presentation = Presentation()
# Load a PPTX file
presentation.LoadFromFile("Input.pptx")
# Or load a PPT file
# presentation.LoadFromFile("Input.ppt")
# Save the file as a PDF
presentation.SaveToFile("Basic_conversion.pdf", FileFormat.PDF)
presentation.Dispose()

Export PowerPoint to PDF with Advanced Settings
Spire.Presentation provides a range of advanced settings that give you control over how the PDF output is generated, making it ideal for both professional use and archival purposes. For example, you can:
- Export a Particular Slide to PDF
- Adjust Slide Size for Optimal PDF Output
- Include Hidden Slides in the Converted PDF
- Generate PDF/A-compliant Files from PowerPoint
Export a Particular Slide to PDF
If you only need to share a specific part of your presentation, Spire.Presentation allows you to extract and convert individual slides to a PDF. This is especially useful for generating slide-specific reports or handouts.
from spire.presentation import *
# Create a Presentation object
presentation = Presentation()
# Load the PowerPoint file
presentation.LoadFromFile("Input.pptx")
# Get the desired slide (e.g., the second slide)
slide = presentation.Slides.get_Item(1)
# Save the slide as a PDF
slide.SaveToFile("Single_slide.pdf", FileFormat.PDF)
presentation.Dispose()

Adjust Slide Size for Optimal PDF Output
To ensure that your PDF meets printing or layout requirements, you can adjust the slide dimensions before conversion. Spire.Presentation lets you set standard slide sizes as well as custom slide dimensions so the output aligns with your document formatting needs.
from spire.presentation import *
# Create a Presentation object
presentation = Presentation()
# Load the PowerPoint file
presentation.LoadFromFile("Input.pptx")
# Set the slide size to a standard slide size like A4
presentation.SlideSize.Type = SlideSizeType.A4
# # Or you can set custom slide size (e.g., 720x540 points)
# presentation.SlideSize.Size = SizeF(720.0, 540.0)
# Fit content to the new slide size
presentation.SlideSizeAutoFit = True
# Save the presentation as a PDF
presentation.SaveToFile("Resized_output.pdf", FileFormat.PDF)
presentation.Dispose()

Include Hidden Slides in the Converted PDF
By default, hidden slides are excluded from conversion. However, if your workflow requires complete documentation, Spire.Presentation enables you to include hidden slides in the output PDF.
from spire.presentation import *
# Create a Presentation object
presentation = Presentation()
# Load the PowerPoint file
presentation.LoadFromFile("Input.pptx")
# Get the SaveToPdfOption object
option = presentation.SaveToPdfOption
# Enable ContainHiddenSlides option
option.ContainHiddenSlides = True
# Save the presentation as a PDF
presentation.SaveToFile("Include_hidden_slides.pdf", FileFormat.PDF)
presentation.Dispose()
Generate PDF/A-compliant Files from PowerPoint
PDF/A is a specialized format intended for long-term digital preservation. If your organization needs to archive presentations in a standards-compliant format, Spire.Presentation allows you to export PDF/A files that conform to archival best practices.
from spire.presentation import *
# Create a Presentation object
presentation = Presentation()
# Load the PowerPoint file
presentation.LoadFromFile("Input.pptx")
# Get the SaveToPdfOption object
option = presentation.SaveToPdfOption
# Set PDF compliance to PDF/A-1a
option.PdfConformanceLevel = PdfConformanceLevel.Pdf_A1A
# Save the presentation as a PDF
presentation.SaveToFile("Pdf_a_output.pdf", FileFormat.PDF)
presentation.Dispose()
Conclusion
Spire.Presentation for Python offers a robust set of features for converting PowerPoint files to PDF with minimal effort and maximum flexibility. Whether you require simple conversions or advanced customization options, this library gives developers full control over the process. From exporting individual slides to generating archival-quality outputs, it’s a comprehensive tool for PowerPoint-to-PDF conversion workflows in Python.
FAQs
Q1: Can I convert PPTX and PPT files without installing Microsoft PowerPoint?
A1: Yes, Spire.Presentation is a standalone library and does not require Microsoft Office or PowerPoint to be installed.
Q2: Does the library support batch conversion of multiple PowerPoint files?
A2: Yes, you can write scripts to loop through multiple files and convert each to PDF programmatically.
Q3: Is PDF/A-1a the only compliance level supported for PPT to PDF/A conversion?
A3: No, Spire.Presentation supports multiple compliance levels for PPT to PDF/A conversion, including PDF/A-1a, PDF/A-2a, PDF/A-3a, PDF/A-1b, PDF/A-2b, and PDF/A-3b.
Get a Free License
To fully experience the capabilities of Spire.Presentation for Python without any evaluation limitations, you can request a free 30-day trial license.
Images in Word documents can break up large blocks of text and make the content more visually interesting. In addition, they can also effectively illustrate complex ideas or concepts that are difficult to explain solely through text. In this article, you will learn how to programmatically add images to a Word document using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Insert an Image in a Word Document in Python
Spire.Doc for Python offers the Paragraph.AppendPicture() method to insert an image into a Word document. The following are the detailed steps.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Add two paragraphs to the section using Section.AddParagraph() method.
- Add text to the paragraphs and set formatting.
- Load an image and add it to a specified paragraph using Paragraph.AppendPicture() method.
- Set width and height for the image using DocPicture.Width and DocPicture.Height properties.
- Set a text wrapping style for the image using DocPicture.TextWrappingStyle property.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Add a seciton
section = document.AddSection()
# Add a paragraph
paragraph1 = section.AddParagraph()
# Add text to the paragraph and set formatting
tr = paragraph1.AppendText("Spire.Doc for Python is a professional Word Python API specifically designed for developers to create, read, write, convert, and compare Word documents with fast and high-quality performance.")
tr.CharacterFormat.FontName = "Calibri"
tr.CharacterFormat.FontSize = 11
paragraph1.Format.LineSpacing = 18
paragraph1.Format.BeforeSpacing = 10
paragraph1.Format.AfterSpacing = 10
# Add another paragraph
paragraph2 = section.AddParagraph()
tr = paragraph2.AppendText("Spire.Doc for Python enables to perform many Word document processing tasks. It supports Word 97-2003 /2007/2010/2013/2016/2019 and it has the ability to convert them to commonly used file formats like XML, RTF, TXT, XPS, EPUB, EMF, HTML and vice versa. Furthermore, it supports to convert Word Doc/Docx to PDF using Python, Word to SVG, and Word to PostScript in high quality.")
# Add text to the paragraph and set formatting
tr.CharacterFormat.FontName = "Calibri"
tr.CharacterFormat.FontSize = 11
paragraph2.Format.LineSpacing = 18
# Add an image to the specified paragraph
picture = paragraph1.AppendPicture("Spire.Doc.jpg")
# Set image width and height
picture.Width = 100
picture.Height = 100
# Set text wrapping style for the image
picture.TextWrappingStyle = TextWrappingStyle.Square
#Save the result document
document.SaveToFile("InsertImage.docx", FileFormat.Docx)
document.Close()

Insert an Image at a Specified Location in a Word document in Python
If you wish to place the image at a specified location in the Word document, you can set its position through the DocPicture.HorizontalPosition and DocPicture.VerticalPosition properties. The following are the detailed steps.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Add a paragraph to the section using Section.AddParagraph() method.
- Add text to the paragraph and set formatting.
- Add an image to the paragraph using Paragraph.AppendPicture() method.
- Set width and height for the image using DocPicture.Width and DocPicture.Height properties.
- Set the horizontal position and vertical position for the image using DocPicture.HorizontalPosition and DocPicture.VerticalPosition properties.
- Set a text wrapping style for the image using DocPicture.TextWrappingStyle property.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
doc = Document()
# Add a section
section = doc.AddSection()
# Add a paragraph to the section
paragraph = section.AddParagraph()
# Add text to the paragraph and set formatting
paragraph.AppendText("The sample demonstrates how to insert an image at a specified location in a Word document.")
paragraph.ApplyStyle(BuiltinStyle.Heading2)
# Add an image to the paragraph
picture = paragraph.AppendPicture("pic.jpg")
# Set image position
picture.HorizontalPosition = 150.0
picture.VerticalPosition = 60.0
# Set image size
picture.Width = 120.0
picture.Height = 180.0
# Set a text wrapping style for the image (note that the position settings are not applicable when the text wrapping style is Inline)
picture.TextWrappingStyle = TextWrappingStyle.Through
# Save the result document
doc.SaveToFile("WordImage.docx", FileFormat.Docx)
doc.Close()

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Text files are a common file type that contain only plain text without any formatting or styles. If you want to apply formatting or add images, charts, tables, and other media elements to text files, one of the recommended solutions is to convert them to Word files.
Conversely, if you want to efficiently extract content or reduce the file size of Word documents, you can convert them to text format. This article will demonstrate how to programmatically convert text files to Word format and convert Word files to text format using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Convert Text (TXT) to Word in Python
Conversion from TXT to Word is quite simple that requires only a few lines of code. The following are the detailed steps.
- Create a Document object.
- Load a text file using Document.LoadFromFile(string fileName) method.
- Save the text file as a Word file using Document.SaveToFile(string fileName, FileFormat fileFormat) method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a TXT file
document.LoadFromFile("input.txt")
# Save the TXT file as Word
document.SaveToFile("TxtToWord.docx", FileFormat.Docx2016)
document.Close()

Convert Word to Text (TXT) in Python
The Document.SaveToFile(string fileName, FileFormat.Txt) method provided by Spire.Doc for Python allows you to export a Word file to text format. The following are the detailed steps.
- Create a Document object.
- Load a Word file using Document.LoadFromFile(string fileName) method.
- Save the Word file in txt format using Document.SaveToFile(string fileName, FileFormat.Txt) method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word file from disk
document.LoadFromFile("Input.docx")
# Save the Word file in txt format
document.SaveToFile("WordToTxt.txt", FileFormat.Txt)
document.Close()

Get a Free License
To fully experience the capabilities of Spire.Doc for Python without any evaluation limitations, you can request a free 30-day trial license.
Comment in Excel is a function that allows users to add extra details or remarks as explanatory notes. Comments can be in the form of text or images. It enables users to provide additional information to explain or supplement the data in specified cells. After adding a comment, users can view the content of the comment by hovering the mouse over the cell with the comment. This feature enhances the readability and comprehensibility of the document, helping readers better understand and handle the data in Excel. In this article, we will show you how to add comments in Excel by using Spire.XLS for Python.
Install Spire.XLS for Python
This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.XLS
If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows
Add Comment with Text in Excel
Spire.XLS for Python allows users to add comment with text in Excel by calling CellRange.AddComment() method. The following are detailed steps.
- Create an object of Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get the first worksheet of this file using Workbook.Worksheets[] property.
- Get the specified cell by using Worksheet.Range[] property.
- Set the author and content of the comment and add them to the obtained cell using CellRange.AddComment() method.
- Set the font of the comment.
- Save the result file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * inputFile = "sample.xlsx" outputFile = "CommentWithAuthor.xlsx" #Create an object of Workbook class workbook = Workbook() #Load the sample file from disk workbook.LoadFromFile(inputFile) #Get the first worksheet sheet = workbook.Worksheets[0] #Get the specified cell range = sheet.Range["B4"] #Set the author and content of the comment author = "Jhon" text = "Emergency task." #Add comment to the obtained cell comment = range.AddComment() comment.Width = 200 comment.Visible = True comment.Text = author + ":\n" + text #Set the font of the comment font = workbook.CreateFont() font.FontName = "Tahoma" font.KnownColor = ExcelColors.Black font.IsBold = True comment.RichText.SetFont(0, len(author), font) #Save the result file workbook.SaveToFile(outputFile, ExcelVersion.Version2013) workbook.Dispose()

Add Comment with Picture in Excel
Additionally, Spire.XLS for Python also enable users to add comment with picture to the specified cell in Excel by using CellRange.AddComment() and ExcelCommentObject.Fill.CustomPicture() methods. The following are detailed steps.
- Create an object of Workbook class.
- Get the first worksheet using Workbook.Worksheets[] property.
- Get the specified cell by using Worksheet.Range[] property and set text for it.
- Add comment to the obtained cell by using CellRange.AddComment() method.
- Load an image and fill the comment with it by calling ExcelCommentObject.Fill.CustomPicture() method.
- Set the height and width of the comment.
- Save the result file using Workbook.SaveToFile() method.
- Python
from spire.xls import * from spire.xls.common import * inputFile = "logo.png" outputFile = "CommentWithPicture.xlsx" #Create an object of Workbook class workbook = Workbook() #Get the first worksheet sheet = workbook.Worksheets[0] #Get the specified cell and set text for it range = sheet.Range["C6"] range.Text = "E-iceblue" #Add comment to the obtained cell comment = range["C6"].AddComment() #Load an image file and fill the comment with it image = Image.FromFile(inputFile) comment.Fill.CustomPicture(image, "logo.png") #Set the height and width of the comment comment.Height = image.Height comment.Width = image.Width comment.Visible = True #Save the result file workbook.SaveToFile(outputFile, ExcelVersion.Version2010) workbook.Dispose()

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.