Convert a Document to Multiple Page Files
Leave feedback
On this page
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 a document into per-page files, use the Converter.convert(file_path, convert_options) method together with the page_number and pages_count attributes on the supported ConvertOptions classes:
page_number: One-based index of the first page to convert.
pages_count: Number of consecutive pages to convert starting from page_number.
To produce one output file per page, loop from 1 to converter.get_document_info().pages_count, updating page_number on each iteration and writing to a different output path. Setting pages_count = 1 ensures each call emits a single page.
Supported ConvertOptions Classes
The following ConvertOptions classes expose the page_number and pages_count attributes used in this topic:
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.
importosfromgroupdocs.conversionimportConverterfromgroupdocs.conversion.filetypesimportImageFileTypefromgroupdocs.conversion.options.convertimportImageConvertOptionsdefconvert_all_document_pages():output_folder="./converted-pages"os.makedirs(output_folder,exist_ok=True)# Instantiate Converter with the input documentwithConverter("./basic-presentation.pptx")asconverter:# Determine the total number of pages in the source documentpages_count=converter.get_document_info().pages_count# Instantiate convert options once and reuse them inside the looppng_convert_options=ImageConvertOptions()png_convert_options.format=ImageFileType.PNGpng_convert_options.pages_count=1# Convert each page to a separate PNG fileforpage_numberinrange(1,pages_count+1):png_convert_options.page_number=page_numberoutput_file=os.path.join(output_folder,f"converted-page-{page_number}.png")converter.convert(output_file,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.
The following example shows how to convert a specific slide in a PPTX presentation and save it as a separate file.
fromgroupdocs.conversionimportConverterfromgroupdocs.conversion.filetypesimportImageFileTypefromgroupdocs.conversion.options.convertimportImageConvertOptionsdefconvert_specific_document_page_to_file():# Instantiate Converter with the input documentwithConverter("./basic-presentation.pptx")asconverter:# Instantiate convert optionspng_convert_options=ImageConvertOptions()# Define the output format as PNGpng_convert_options.format=ImageFileType.PNG# Specify the single page to convertpng_convert_options.page_number=3png_convert_options.pages_count=1# Save the converted page to a fileconverter.convert("./slide-3.png",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.
If you need the converted page as an in-memory buffer (e.g., to forward it to another API without touching the filesystem afterwards), convert the page to a file first and then read it into a BytesIO object:
importiofromgroupdocs.conversionimportConverterfromgroupdocs.conversion.filetypesimportImageFileTypefromgroupdocs.conversion.options.convertimportImageConvertOptionsdefconvert_specific_document_page_to_stream():page_number_to_convert=5output_file=f"./slide-{page_number_to_convert}.png"# Instantiate Converter with the input documentwithConverter("./basic-presentation.pptx")asconverter:# Instantiate convert optionspng_convert_options=ImageConvertOptions()# Define the output format as PNGpng_convert_options.format=ImageFileType.PNG# Specify the single page to convertpng_convert_options.page_number=page_number_to_convertpng_convert_options.pages_count=1# Convert and save the page to a file on diskconverter.convert(output_file,png_convert_options)# Load the converted page into an in-memory stream for downstream usewithopen(output_file,"rb")asfile_handle:page_stream=io.BytesIO(file_handle.read())# page_stream now holds the PNG bytes and can be passed to any consumerprint(f"Loaded {page_stream.getbuffer().nbytes} bytes into memory")if__name__=="__main__":convert_specific_document_page_to_stream()
basic-presentation.pptx is the sample file used in this example. Click here to download it.