Download splitPDF.py from PKUfudawei/PDG2024: direct link, hf CLI and curl.
- Browser
- Download file 826 Bytes
-
https://huggingface.co/datasets/PKUfudawei/PDG2024/resolve/main/splitPDF.py
- Command line
-
hf download hf://datasets/PKUfudawei/PDG2024/splitPDF.py
-
curl -L -o splitPDF.py https://huggingface.co/datasets/PKUfudawei/PDG2024/resolve/main/splitPDF.py
826 Bytes
| import os | |
| from PyPDF2 import PdfReader, PdfWriter | |
| def split_pdf(input_path, output_dir="output", max_pages=99): | |
| os.makedirs(output_dir, exist_ok=True) | |
| reader = PdfReader(input_path) | |
| total_pages = len(reader.pages) | |
| file_index = 0 | |
| for start in range(0, total_pages, max_pages): | |
| writer = PdfWriter() | |
| end = min(start + max_pages, total_pages) | |
| for i in range(start, end): | |
| writer.add_page(reader.pages[i]) | |
| output_path = os.path.join(output_dir, f"{file_index}.pdf") | |
| with open(output_path, "wb") as f: | |
| writer.write(f) | |
| print(f"Saved: {output_path} (pages {start}-{end-1})") | |
| file_index += 1 | |
| if __name__ == "__main__": | |
| input_pdf = "PhysRevD.110.030001.pdf" | |
| split_pdf(input_pdf, output_dir=input_pdf.replace('.pdf', '')) |