Convert a Document to Multiple Page Files

This documentation topic covers the conversion of a single multi-page document into individual page files. The following diagram illustrates the process of converting a multi-page file into separate pages:

flowchart LR
    %% Nodes
    A["Input Document"]
    B["Conversion"]
    C["Converted Page 1"]
    D["Converted Page 2"]
    E["Converted Page N"]

    %% Edge connections between nodes
    A --> B --> C
    B --> D
    B --> E

To convert and save a document by page, use the following methods of the Converter class:

  • convert_by_page(output_path, convert_options): Converts each page of a document to the specified output format and saves them individually to disk.
  • convert_by_page(file_path, page_number, convert_options): Converts a specific page of a document and saves it to the specified output file path.
  • convert_by_page(stream, page_number, convert_options): Converts a specific page of a document and saves it to a provided stream.

Supported ConvertOptions Classes

The following ConvertOptions classes can be used with the convert_by_page method:

  • PdfConvertOptions – Options for converting to PDF format.
  • ImageConvertOptions – Options for converting to Image formats (e.g., PNG, JPEG).
  • WordProcessingConvertOptions – Options for converting to Word Processing formats.
  • SpreadsheetConvertOptions – Options for converting to Spreadsheet formats.
  • PresentationConvertOptions – Options for converting to Presentation formats.
  • WebConvertOptions – Options for converting to Web formats (e.g., HTML).
  • EBookConvertOptions – Options for converting to EBook formats (e.g., EPUB, MOBI).
  • DiagramConvertOptions – Options for converting to Diagram formats (e.g., VSDX).
  • PageDescriptionLanguageConvertOptions – Options for converting to Page Description Language formats (e.g., PostScript).
  • CadConvertOptions – Options for converting to CAD formats (e.g., DWG).
  • ThreeDConvertOptions – Options for converting to 3D formats.
  • FinanceConvertOptions – Options for converting to Finance formats (e.g., XBRL).

Example 1: Convert All Pages of a Document and Save Output to a Folder

The following example demonstrates how to convert each slide in a PPTX presentation to a PNG image and save the output images to a specified folder.

The file name template for the output files is converted-page-{page number}.{output file extension}. In this example, the first slide will be saved as converted-page-1.png.

from groupdocs.conversion import Converter
from groupdocs.conversion.filetypes import ImageFileType
from groupdocs.conversion.options.convert import ImageConvertOptions

def convert_all_document_pages():
    # Instantiate Converter with the input document 
    with Converter("./basic-presentation.pptx") as converter:
        # Instantiate convert options 
        png_convert_options = ImageConvertOptions()
        # Define the output format as PNG
        png_convert_options.format = ImageFileType.PNG
        
        # Convert all pages and save to the output folder
        converter.convert_by_page("./converted-pages", png_convert_options)    

if __name__ == "__main__":
    convert_all_document_pages()

basic-presentation.pptx is the sample file used in this example. Click here to download it.

converted-pages is the output folder path for the converted pages. Click here to download it.

Example 2: Convert a Specific Page and Save Output to a File

Tip
Find out how to get the number of document pages in the Getting Document Information documentation topic.

The following example shows how to convert a specific slide in a PPTX presentation and save it as a separate file.

from groupdocs.conversion import Converter
from groupdocs.conversion.filetypes import ImageFileType
from groupdocs.conversion.options.convert import ImageConvertOptions

def convert_specific_document_page_to_file():
    # Instantiate Converter with the input document 
    with Converter("./basic-presentation.pptx") as converter:
        # Instantiate convert options 
        png_convert_options = ImageConvertOptions()
        # Define the output format as PNG
        png_convert_options.format = ImageFileType.PNG
        
        # Set the page number to convert and save it to a file
        page_number_to_convert = 3
        converter.convert_by_page("./slide-3.png", page_number_to_convert, png_convert_options)    

if __name__ == "__main__":
    convert_specific_document_page_to_file()

basic-presentation.pptx is the sample file used in this example. Click here to download it.

slide-3.png is the expected output PNG image. Click here to download it.

Example 3: Convert a Specific Page and Save Output to a Stream

Tip
Find out how to get the number of document pages in the Getting Document Information documentation topic.

As an alternative, you can specify the stream to save the converted PNG image:

from groupdocs.conversion import Converter
from groupdocs.conversion.filetypes import ImageFileType
from groupdocs.conversion.options.convert import ImageConvertOptions

def convert_specific_document_page_to_file():
    # Instantiate Converter with the input document 
    with Converter("./basic-presentation.pptx") as converter:
        # Instantiate convert options 
        png_convert_options = ImageConvertOptions()
        # Define the output format as PNG
        png_convert_options.format = ImageFileType.PNG
        
        # Set the page number to convert and save it to a file
        page_number_to_convert = 5
        with open(f"./slide-{page_number_to_convert}.png", "w+b") as page_stream:
            converter.convert_by_page(page_stream, page_number_to_convert, png_convert_options)    

if __name__ == "__main__":
    convert_specific_document_page_to_file()

basic-presentation.pptx is the sample file used in this example. Click here to download it.

slide-5.png is the expected output PNG image. Click here to download it.