How to PDF File into multiple PDFs in Python

Python Document Splitting - Split PDF File into Multiple PDF Files using Python.

PDF (Portable Document Format) is a widely used file format for documents that need to be shared, printed, or archived. Are you tired of dealing with large PDF files that contain multiple documents or sections? Do you find it difficult to extract specific pages or sections from a single PDF file as separate documents? Splitting a PDF file into multiple smaller files can greatly simplify your document management tasks. In this article, we will explore how to split a PDF file into multiple PDF files in Python using GroupDocs.Merger Cloud SDK for Python.

The following topics shall be covered in this article:

Python REST API to Split PDF into Pages and SDK Installation

GroupDocs.Merger Cloud SDK for Python is a powerful and feature-rich software development kit that allows you to manipulate PDF files programmatically. It provides various functionalities, including splitting, merging, rotating, and rearranging a collection of pages in supported document formats. The SDK can be integrated into a Python-based application to simplify your development process and enhance productivity.

You can install the Merger API in your Python application with PIP from PyPI by running the following command in the terminal:

pip install groupdocs-merger-cloud

Next, you need to sign up for a free trial account or purchase a subscription plan on the GroupDocs website and get your API key. Once you have the Client Id and Client Secret, add below code snippet to a Python-based application:

How to Split PDF Pages into Separate PDF Files in Python

To split a PDF file into multiple PDF files using the GroupDocs.Merger Cloud SDK for Python, follow these steps:

Upload the Files

Firstly, upload the PDF file to the cloud using the code example given below:

As a result, the uploaded files will be available in the files section of your dashboard on the cloud.

Split PDF into Individual Pages in Python

In this section, we will write steps and an example code snippet to split PDF pages into separate PDF files programmatically as given below:

  • Firstly, create an instance of the DocumentApi class.
  • Secondly, create an instance of the SplitOptions class.
  • Thirdly, create an instance of the FileInfo class.
  • Provide input file path as a parameter to FileInfo.
  • Next, provide the output directory path.
  • Set specific page numbers in a comma-separated array.
  • Now, set PDF split mode to Pages to split page numbers.
  • Then, create an instance of SplitRequest class and pass the SplitOptions parameter.
  • Finally, call the DocumentAPI.split() method and pass the SplitRequest parameter to get the results.

The following code snippet shows how to split PDF files in Python using REST API:

Download the File

The above code sample will save the the separated file on the cloud. You can download it using the following code sample:

That’s it!

Split PDF Document into Separate Files by Applying Range Filter

You can separate PDF files by providing a page range mode and filter programmatically by following the steps given below:

  • Firstly, create an instance of the DocumentApi class.
  • Secondly, create an instance of the SplitOptions class.
  • Thirdly, create an instance of the FileInfo class.
  • Provide input file path as a parameter to FileInfo.
  • Next, provide the output directory path as “python-testing”.
  • Set start_page_number and end_page_number values.
  • Next, set page range_mode to OddPages.
  • Now, set PDF split mode to Pages to split page numbers.
  • Then, create an instance of SplitRequest class and pass the SplitOptions parameter.
  • Finally, call the DocumentAPI.split() method and pass the SplitRequest parameter to get the results.

The following code snippet shows how to split a PDF file by applying a filter in Python using REST API:

Split PDF Pages into Multiple Files in Python by Applying Array Filter

In this section, we will write steps and an example code snippet to split PDF files into multipage PDF files programmatically:

  • Firstly, create an instance of the DocumentApi class.
  • Secondly, create an instance of the SplitOptions class.
  • Thirdly, create an instance of the FileInfo class.
  • Provide input file path as a parameter to FileInfo.
  • Next, provide the output directory path as “python-testing”.
  • Then, set the page collection in array format.
  • Set PDF split mode to Intervals to split PDF files.
  • Then, create an instance of SplitRequest class and pass the SplitOptions parameter.
  • Finally, call the DocumentAPI.split() method and pass the SplitRequest parameter to get the results.

The following code snippet shows how to split PDF files into multiple PDF files in Python using REST API:

Free Online PDF Splitter

How to split PDF files online for free? Please try the following online PDF splitter tool to split PDF documents for free. This document splitter online tool is developed using the above-mentioned REST API.

Conclusion

In conclusion, splitting a PDF file into multiple PDF files using the GroupDocs.Merger Cloud SDK for Python provides a convenient way to manage and manipulate your PDF documents. The following is what you have learned in this article:

  • how to split one PDF file into multiple files using Python on the cloud;
  • programmatically upload and download the files in Python on the cloud;
  • split PDF files into multiple files in Python by using a pages range filter;
  • and split PDF files for free using an online PDF splitter.

Additionally, we also provide an API Reference section that lets you visualize and communicate with our APIs directly through the browser. Python SDK’s complete source code is freely available on GitHub. Please check the GroupDocs.Merger Cloud SDK for Python Examples here.

Moreover, we suggest you follow our Getting Started guide for detailed steps and API usage.

Finally, we keep writing new blog posts on different document operations using REST API. So, please get in touch for the regular updates.

Ask a question

If you have any questions about the PDF document splitter API, please feel free to ask us on the Free Support Forum.

FAQs

Can I split a PDF file into multiple files based on specific pages?

Yes, using the GroupDocs.Merger Cloud SDK for Python, you can define the range of pages to extract and split a PDF into separate PDF files.

Does GroupDocs.Merger Cloud SDK for Python support other document formats besides PDF?

Yes, GroupDocs.Merger Cloud SDK for Python supports various document formats, including DOCX, XLSX, PPTX, and more. You can perform similar operations on these file types as well.

Is it possible to merge the split PDF files back into a single file if needed?

Yes, GroupDocs.Merger Cloud SDK for Python also supports merging PDF files. You can easily combine the split PDF files into a single document when required.

Does the GroupDocs.Merger Cloud SDK for Python preserve the original formatting of the PDF files during the splitting process?

Yes, the SDK maintains the original formatting and layout of the PDF files while splitting them into multiple files.

See Also

For further information on related topics, we suggest taking a look at the following articles: