What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
To export selected pages from a PDF in Python, open the file with PyMuPDF and call Document.select(), or use pypdf to add chosen pages to a new PdfWriter. Both libraries use zero-based page indexes: page 1 is index 0. Convert reader-facing page numbers before selecting, validate them against the source page count, and write to a separate output file.
Choose the page numbers you want to export
First decide whether your input is expressed as page numbers a person sees—usually starting at 1—or as indexes expected by the Python library. PyMuPDF’s selection API and pypdf’s reader pages use zero-based indexes. Thus, for a request to export PDF pages 1, 3, and 5, the indexes are [0, 2, 4].
Do not assume a printed page label inside a document is the same as its physical position. A PDF may display labels such as “iv” or “12,” while the page’s position in the file remains a zero-based index. The APIs described here select by position; if your task refers to printed labels, map those labels to physical positions first.
Selection order matters: PyMuPDF documents that the supplied sequence controls output order and can repeat an index. You can therefore create an output with pages reordered or duplicated when that is intentional. For a normal extraction, provide each requested page once and in the desired order.
#1 Best Overall
- EDIT text, images & designs in PDF documents. ORGANIZE PDFs. Convert PDFs to Word, Excel & ePub.
- READ and Comment PDFs – Intuitive reading modes & document commenting and mark up.
- CREATE, COMBINE, SCAN and COMPRESS PDFs
- FILL forms & Digitally Sign PDFs. PROTECT and Encrypt PDFs
- LIFETIME License for 1 Windows PC or Laptop. 5GB MobiDrive Cloud Storage Included.
Use PyMuPDF for a compact selection
PyMuPDF’s Document.select() keeps the indexes in the supplied sequence. Its documentation describes the operation as: “Document.select() shrinks a PDF down to selected pages.” The following script accepts human page numbers, checks them, selects the pages, saves a separate PDF, and verifies the resulting page count.
- Install the library:
python -m pip install pymupdf. - Save this as
extract_pages.py:
from pathlib import Path
import pymupdf
source_path = Path("input.pdf")
output_path = Path("selected-pages.pdf")
# Human-facing, one-based PDF page numbers to export.
requested_pages = [1, 3, 5]
if not source_path.is_file():
raise FileNotFoundError(f"PDF not found: {source_path}")
if not requested_pages:
raise ValueError("Select at least one page")
if output_path.resolve() == source_path.resolve():
raise ValueError("Choose an output path different from the source")
with pymupdf.open(source_path) as doc:
page_count = doc.page_count
if any(not isinstance(n, int) or isinstance(n, bool) for n in requested_pages):
raise TypeError("Page numbers must be integers")
invalid = [n for n in requested_pages if n < 1 or n > page_count]
if invalid:
raise ValueError(
f"Page numbers {invalid} are outside the valid range 1..{page_count}"
)
indexes = [n - 1 for n in requested_pages]
doc.select(indexes)
doc.save(output_path)
with pymupdf.open(output_path) as result:
if result.page_count != len(requested_pages):
raise RuntimeError(
f"Expected {len(requested_pages)} pages, got {result.page_count}"
)
print(f"Saved {result.page_count} pages to {output_path}")
Replace [1, 3, 5] with the pages you need. For a PDF containing fewer than five pages, the validation reports the valid range instead of attempting an invalid selection. If you already have zero-based indexes, omit the conversion and validate each index against 0 <= i < page_count. PyMuPDF documents that an empty sequence or an out-of-range selection raises ValueError.
The script checks that the destination differs from the source so the original is not inadvertently replaced. For production workflows, also decide whether an existing output should be overwritten; choose a unique output path or add an explicit overwrite policy rather than silently replacing an important file.
Use pypdf when you want to build a destination PDF
With pypdf, read the source, add each selected page to a new writer, then write the destination. This pattern makes the construction of the output explicit and fits workflows that already use the reader/writer API.
- Install pypdf:
python -m pip install pypdf. - Save this as
extract_with_pypdf.py:
from pathlib import Path
from pypdf import PdfReader, PdfWriter
source_path = Path("input.pdf")
output_path = Path("selected-pages.pdf")
requested_pages = [1, 3, 5] # Human-facing, one-based page numbers.
if not source_path.is_file():
raise FileNotFoundError(f"PDF not found: {source_path}")
if not requested_pages:
raise ValueError("Select at least one page")
if output_path.resolve() == source_path.resolve():
raise ValueError("Choose an output path different from the source")
reader = PdfReader(source_path)
page_count = len(reader.pages)
if any(not isinstance(n, int) or isinstance(n, bool) for n in requested_pages):
raise TypeError("Page numbers must be integers")
invalid = [n for n in requested_pages if n < 1 or n > page_count]
if invalid:
raise ValueError(
f"Page numbers {invalid} are outside the valid range 1..{page_count}"
)
writer = PdfWriter()
for page_number in requested_pages:
writer.add_page(reader.pages[page_number - 1])
with output_path.open("wb") as output:
writer.write(output)
check = PdfReader(output_path)
if len(check.pages) != len(requested_pages):
raise RuntimeError(
f"Expected {len(requested_pages)} pages, got {len(check.pages)}"
)
print(f"Saved {len(check.pages)} pages to {output_path}")
The pypdf APIs use zero-based page access, and PdfWriter.add_page() appends a page to the output document. If your code already has indexes, use reader.pages[index] after checking the range. The pypdf merging guide also shows selecting pages by index with writer.append(); its cited example is for pypdf 6.3.0, so check the documentation for your installed version before relying on version-specific range or tuple syntax.
Rank #2
- Edit PDFs with Ease. Modify text, images, and layouts directly within your PDF documents.
- Convert & Organize. Export PDFs to Word, Excel, or ePub, and organize files with ease.
- Read & Annotate. Enjoy intuitive reading modes and powerful tools to comment, highlight, and mark up PDFs.
- Create & Manage PDFs. Create new PDFs, combine multiple files, scan documents, and compress for easy sharing.
- Fill & Sign Forms. Complete forms and digitally sign documents with secure e-signature tools.
PyMuPDF or pypdf: which approach fits?
| Need | PyMuPDF | pypdf |
|---|---|---|
| Selection style | Call doc.select(indexes) on the opened document. |
Add chosen reader.pages[index] objects to a PdfWriter, then write it. |
| Indexing | Zero-based: first page is index 0. | Zero-based: first page is index 0. |
| Order and duplicates | The supplied sequence controls order and may repeat indexes, per PyMuPDF’s basics guide. | The add-page loop writes pages in the order you add them; repeat an addition only if duplicate output pages are intended. |
| Document structures | PyMuPDF’s tutorial says links, annotations, and bookmarks are retained when they remain valid because they point to a selected page or an external resource. | The cited pypdf material establishes page addition and writing; equivalent preservation details are not stated in those sources. |
Choose based on the API your project already uses, whether you need explicit destination-page construction, and whether ordering, duplicates, links, annotations, or bookmarks matter. The cited documentation does not establish a universal speed or output-quality winner. When document structures matter, inspect the produced PDF rather than assuming all references to omitted pages remain useful.
Validate the result and protect the source
- Confirm the input is the intended file. Check the path and open the source successfully before selecting pages.
- Validate the selection. Reject an empty list if at least one page is required, and check every one-based page number against the source page count before converting it to an index.
- Write to another path. A separate destination avoids accidental source replacement and leaves the original available for recovery.
- Check the output count. Reopen the output or inspect its page count. It should equal the number of requested entries, including intentional duplicates.
- Inspect important references. Open the output and test bookmarks, annotations, and links that matter, particularly links to pages excluded from the selection.
For a quick count, PyMuPDF supports doc.page_count and len(doc); pypdf’s reader pages can be counted with len(reader.pages). Count validation confirms that the expected number of pages was written, not that the selected pages are semantically the right ones—visually inspect the result when page identity is important.
Troubleshoot common extraction problems
“No such file” or the wrong PDF opens
The script resolves paths from its current working directory, which may differ from the directory containing the Python file. Use an absolute path or run the script from the expected directory, and check source_path.is_file() before opening.
Index or range errors
A page-numbering mismatch is the usual cause: the user says page 1, but index 1 means the second page. Convert one-based page numbers using n - 1; reject values below 1 or above the actual page count before calling the library. If working directly with indexes, require 0 <= i < page_count.
The output has no pages
An empty selection cannot produce the requested nonempty extraction. Check how the page list is assembled—for example, whether parsing a range accidentally returned nothing—and reject an empty list when at least one page is required.
Rank #3
- EVERY PDF TOOL UNLOCKED - 30+ tools in one app: edit text and images, convert, merge, split, compress, sign, OCR, redact, watermark, batch process, and more. No feature gates, no upsells, nothing held back.
- PAY ONCE, OWN FOREVER — A one-time purchase, not a subscription. Other apps runs $240/year — Scrivar is yours for life, with free updates included.
- UNLIMITED eSIGN, BUILT IN — Send contracts and forms for signature and track every step. Recipients sign in their browser with no account or app needed. Replace DocuSign and save hundreds a year.
- PC, MAC, AND WEB — Install on any Win 10/11 PC or macOS 11+ Mac (Intel or Apple Silicon), or work in your browser at scrivar.com. Same tools, same account, everywhere you work.
- OCR + FULL OFFICE CONVERSION — Turn scanned documents into searchable, selectable text, and convert PDFs to and from Word, Excel, and PowerPoint with formatting kept intact.
The saved PDF opens, but a bookmark or link is missing or misleading
Selection can make internal references to omitted pages invalid. PyMuPDF documents retention for links, annotations, and bookmarks that remain valid by pointing to a selected page or external resource; that is not a promise that references to excluded pages will work. Inspect the output and adjust expectations or the selection for documents that rely on those references.
Another library uses a different page-number convention
Check the specific interface rather than assuming all PDF tools use the same numbering. For example, pdfplumber’s command-line --pages argument uses one-indexed page numbers; that CLI convention does not establish the convention for every Python API.
Or skip the browser setup
ScreenshotNeo is a website screenshot API, not a library for extracting pages from an existing PDF. If your actual task is to capture a web page, one GET request can return a screenshot; the example below saves a WebP capture of Stripe. See the ScreenshotNeo API documentation for setup and options.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
For web captures, ScreenshotNeo removes cookie banners, newsletter popups, and chat widgets before the shot; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. These capabilities do not replace selecting pages from a PDF already on disk. Learn about ScreenshotNeo, or sign up for 1,000 free screenshots a month with no card.
Frequently asked questions
Can I export pages in a different order?
Yes. Supply or add the selected pages in the order you want them to appear in the output.
Can I include the same page more than once?
PyMuPDF’s selection sequence can repeat indexes. With pypdf, adding the same reader page again appends another copy.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

