pypdf
Use scripts/pypdf_cli.py for deterministic PDF operations instead of ad-hoc pypdf snippets.
Workflow
- 1. Confirm the PDF file path is accessible locally.
- Run the desired command (inspect, extract-text, extract-pages, merge, split, rotate).
- Inspect text output or the resulting PDF file.
Command Guide
- - Inspect metadata and page count:
-
python scripts/pypdf_cli.py info --input <file.pdf>
-
python scripts/pypdf_cli.py extract-text --input <file.pdf>
- - Extract text from specific pages (0-indexed):
-
python scripts/pypdf_cli.py extract-text --input <file.pdf> --pages 0 1 2
- - Split PDF into individual pages:
-
python scripts/pypdf_cli.py split --input <file.pdf> --output-dir <dir/>
- - Extract a page range into a new PDF:
-
python scripts/pypdf_cli.py extract-pages --input <file.pdf> --pages 0 1 2 --output <out.pdf>
-
python scripts/pypdf_cli.py merge --inputs <a.pdf> <b.pdf> <c.pdf> --output <merged.pdf>
-
python scripts/pypdf_cli.py rotate --input <file.pdf> --angle 90 --output <rotated.pdf>
-
--angle must be 90, 180, or 270.
- Optionally restrict to specific pages with
--pages 0 2.
Operational Rules
- - Pages are always 0-indexed in all commands.
- For
extract-text, output goes to stdout; redirect to a file when needed. - Require explicit
--output for commands that write a new PDF. - Install dependency if missing:
pip install pypdf.
pypdf
使用 scripts/pypdf_cli.py 进行确定性的 PDF 操作,而非临时编写 pypdf 代码片段。
工作流程
- 1. 确认 PDF 文件路径在本地可访问。
- 运行所需命令(检查、提取文本、提取页面、合并、拆分、旋转)。
- 检查文本输出或生成的 PDF 文件。
命令指南
- python scripts/pypdf_cli.py info --input <文件.pdf>
- python scripts/pypdf_cli.py extract-text --input <文件.pdf>
- python scripts/pypdf_cli.py extract-text --input <文件.pdf> --pages 0 1 2
- python scripts/pypdf_cli.py split --input <文件.pdf> --output-dir <目录/>
- python scripts/pypdf_cli.py extract-pages --input <文件.pdf> --pages 0 1 2 --output <输出.pdf>
- python scripts/pypdf_cli.py merge --inputs
--output <合并后.pdf>
- python scripts/pypdf_cli.py rotate --input <文件.pdf> --angle 90 --output <旋转后.pdf>
- --angle 必须为 90、180 或 270。
- 可选地使用 --pages 0 2 限制特定页面。
操作规则
- - 所有命令中的页面均从 0 开始索引。
- 对于 extract-text,输出到标准输出;需要时重定向到文件。
- 对于写入新 PDF 的命令,需要显式指定 --output。
- 如缺少依赖,请安装:pip install pypdf。