---
title: Splitting a pdf to single pages with PyPDF2
description: Splitting a pdf to single pages with PyPDF2
---

[Skip to content](https://www.connysoderholm.com/blog/splitting-a-pdf-to-single-pages-with-pypdf2#main-content)

![Conny-Söderholm](https://www.connysoderholm.com/hubfs/Conny-S%C3%B6derholm.svg)

- [Home](https://www.connysoderholm.com/)
- [Courses](https://www.connysoderholm.com/courses)
- [Articles](https://www.connysoderholm.com/blog)
- [Contact Me](https://www.connysoderholm.com/contact-me)

Open main navigation

Close main navigation

- [Home](https://www.connysoderholm.com/)
- [Courses](https://www.connysoderholm.com/courses)
- [Articles](https://www.connysoderholm.com/blog)
- [Contact Me](https://www.connysoderholm.com/contact-me)

 December 13, 2019

# Splitting a pdf to single pages with PyPDF2

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm)

Share: [facebook-f icon](http://www.facebook.com/share.php?u=https://www.connysoderholm.com/blog/splitting-a-pdf-to-single-pages-with-pypdf2) [linkedin-in icon](http://www.linkedin.com/shareArticle?mini=true&url=https://www.connysoderholm.com/blog/splitting-a-pdf-to-single-pages-with-pypdf2) [Twitter icon](https://twitter.com/intent/tweet?url=https://www.connysoderholm.com/blog/splitting-a-pdf-to-single-pages-with-pypdf2) [pinterest-p icon](http://pinterest.com/pin/create/link/?url=https://www.connysoderholm.com/blog/splitting-a-pdf-to-single-pages-with-pypdf2) [envelope icon](mailto:?body=https://www.connysoderholm.com/blog/splitting-a-pdf-to-single-pages-with-pypdf2)

# Splitting Pdfs with Python

In this post, we are going to have a look at how to split all pages from a single pdf into one-page pdf files. Splitting a pdf into several pages can easily be done with almost any pdf tool worth its salt. However, splitting a pdf into single pages is a manual operation, and if you have to do it on several pdfs an automated tool makes sense. This is where PyPDF2 comes in handy. If you just want the complete code without all the fancy explanations, you can find it at the end.

## Preparations

If you haven't done so already, fire up your command prompt, PowerShell or terminal and install PyPDF2 with pip. 

```
pip install pypdf2
```

Currently I am running 32-bit Python 3.8 with PyPDF2 version 1.26.0 on Windows 10. The code works on this setup, and probably also for other OS'es. 

## Code line by line

### Imports

We start with importing PdfFileWriter and PdfFileReader so that we can read the existing pdf and later write new pdfs. We also need to import sys so that we can check what files we have have in our working directory.

```
from PyPDF2 import PdfFileWriter, PdfFileReaderimport os
```

### Getting the pdf files to split

First we do a list comprehension in `os.listdir(".")`if the provided path is a file `os.path.isfile(f)`. After that we filter out all the pdf files from the list `files`with `files = list(filter(lambda f: f.lower().endswith((".pdf")), files))`.

```
files = [f for f in os.listdir(".") if os.path.isfile(f)]
files = list(filter(lambda f: f.lower().endswith((".pdf")), files))
```

### Splitting and creating new pdf

Now it is time to process all our pdf files. We go through each of our pdf in files with a for loop `for pdf in files:`. We then open the pdf `with open(pdf, "rb") as f:` and load each pdf into a PdfFileReader object with `inputpdf = PdfFileReader(f)`.

Now it is time to start the splitting. With another for loop, we loop through all pages in the pdf. You can get the number of pages with `numPages`. We create a PdfFileWriter object named output and add the first page with `getPage(i)`. We name the output pdf with the original name, add -Page and the page number. `name = pdf[:-4]+"-Page "+str(i)+".pdf"`. Finally, we save the output.

```
with open(name, "wb") as outputStream:     output.write(outputStream)
```

## Complete code

```
from PyPDF2 import PdfFileWriter, PdfFileReader

import os
files = [f for f in os.listdir(".") if os.path.isfile(f)]

files = list(filter(lambda f: f.lower().endswith((".pdf")), files))

for pdf in files:

    with open(pdf, "rb") as f:

        inputpdf = PdfFileReader(f)

        for i in range(inputpdf.numPages):

            output = PdfFileWriter()

            output.addPage(inputpdf.getPage(i))

            name = pdf[:-4]+"-Page "+str(i)+".pdf"

            with open(name, "wb") as outputStream:

                output.write(outputStream)
```

 

[Python](https://www.connysoderholm.com/blog/tag/python), [pypdf2](https://www.connysoderholm.com/blog/tag/pypdf2)

## Related posts

[Python](https://www.connysoderholm.com/blog/tag/python), [pypdf2](https://www.connysoderholm.com/blog/tag/pypdf2)

## [Count the number of pages in a PDF file with Python and PyPDF2](https://www.connysoderholm.com/blog/count-the-number-of-pages-in-a-pdf-file-with-python-and-pypdf2)

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) 

 August 19, 2020

Counting the number of PDF pages Do you need to count the total number of pages in your pdf files?

[Read more](https://www.connysoderholm.com/blog/count-the-number-of-pages-in-a-pdf-file-with-python-and-pypdf2)

[Python](https://www.connysoderholm.com/blog/tag/python), [pypdf2](https://www.connysoderholm.com/blog/tag/pypdf2)

## [Rotate PDF files with PyPdf2 and Tkinter!](https://www.connysoderholm.com/blog/rotate-pdf-files-with-pypdf2-and-tkinter)

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) 

 January 24, 2020

Rotating PDF files with PyPDF2 and Tkinter Introduction Sometimes we need simple tools to get the...

[Read more](https://www.connysoderholm.com/blog/rotate-pdf-files-with-pypdf2-and-tkinter)

[Python](https://www.connysoderholm.com/blog/tag/python), [pypdf2](https://www.connysoderholm.com/blog/tag/pypdf2)

## [Searching for text in PDF files with pypdf2](https://www.connysoderholm.com/blog/search-pdf-files-with-pypdf2)

![Picture of Conny Söderholm](https://app.hubspot.com/settings/avatar/c60eb763b898c1f4c2e54f9401f3aef1) [Conny Söderholm](https://www.connysoderholm.com/blog/author/conny-soderholm) 

 June 29, 2018

Searching for text in PDF files with pypdf2 Portable Document Format (PDF) is wonderful as long as...

[Read more](https://www.connysoderholm.com/blog/search-pdf-files-with-pypdf2)

[linkedin-in icon](https://www.linkedin.com/in/connysoderholm) [Follow us on Facebook](https://www.youtube.com/@connysoderholm1473)

- [Home](https://www.connysoderholm.com)
- [Courses](https://www.connysoderholm.com/courses)
- [Articles](https://www.connysoderholm.com/blog)
- [Contact Me](https://www.connysoderholm.com/contact-me)
- [Privacy Policy](https://www.connysoderholm.com/privacy-policy)
- [Disclaimer](https://www.connysoderholm.com/disclaimer)

---

[![Conny Söderholm Logo](https://www.connysoderholm.com/hubfs/Conny-S%C3%B6derholm.svg "Conny Söderholm Logo")](http://Conny%20Söderholm)

Copyright © 2026 Conny Söderholm

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Conny Söderholm",
    "url" : "https://www.connysoderholm.com/blog/author/conny-soderholm"
  },
  "dateModified" : "2023-08-01T04:14:13.231Z",
  "datePublished" : "2019-12-13T17:54:52.000Z",
  "headline" : "Splitting a pdf to single pages with PyPDF2",
  "mainEntityOfPage" : {
    "@id" : "https://www.connysoderholm.com/blog/splitting-a-pdf-to-single-pages-with-pypdf2",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://www.connysoderholm.com/hubfs/Conny-S%C3%B6derholm.svg"
    },
    "name" : "Conny Söderholm"
  }
}
```