PDF Processing

PDF Processing

Teaches an AI agent to read, merge, split, rotate, watermark, create, fill and encrypt PDF files, and OCR scanned ones, using Python libraries and command-line tools.

Skill files12 files

scripts/

SKILL.md315 lines
1
---
2
name: pdf
3
description: Use this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple PDFs into one, splitting PDFs apart, rotating pages, adding watermarks, creating new PDFs, filling PDF forms, encrypting/decrypting PDFs, extracting images, and OCR on scanned PDFs to make them searchable. If the user mentions a .pdf file or asks to produce one, use this skill.
4
license: Complete terms in LICENSE.txt
5
---
6
7
# PDF Processing Guide
8
9
## Overview
10
11
This guide covers essential PDF processing operations using Python libraries and command-line tools. For advanced features, JavaScript libraries, and detailed examples, see REFERENCE.md. If you need to fill out a PDF form, read FORMS.md and follow its instructions.
12
13
## Quick Start
14
15
```python
16
from pypdf import PdfReader, PdfWriter
17
18
# Read a PDF
19
reader = PdfReader("document.pdf")
20
print(f"Pages: {len(reader.pages)}")
21
22
# Extract text
23
text = ""
24
for page in reader.pages:
25
text += page.extract_text()
26
```
27
28
## Python Libraries
29
30
### pypdf - Basic Operations
31
32
#### Merge PDFs
33
```python
34
from pypdf import PdfWriter, PdfReader
35
36
writer = PdfWriter()
37
for pdf_file in ["doc1.pdf", "doc2.pdf", "doc3.pdf"]:
38
reader = PdfReader(pdf_file)
39
for page in reader.pages:
40
writer.add_page(page)
41
42
with open("merged.pdf", "wb") as output:
43
writer.write(output)
44
```
45
46
#### Split PDF
47
```python
48
reader = PdfReader("input.pdf")
49
for i, page in enumerate(reader.pages):
50
writer = PdfWriter()
51
writer.add_page(page)
52
with open(f"page_{i+1}.pdf", "wb") as output:
53
writer.write(output)
54
```
55
56
#### Extract Metadata
57
```python
58
reader = PdfReader("document.pdf")
59
meta = reader.metadata
60
print(f"Title: {meta.title}")
61
print(f"Author: {meta.author}")
62
print(f"Subject: {meta.subject}")
63
print(f"Creator: {meta.creator}")
64
```
65
66
#### Rotate Pages
67
```python
68
reader = PdfReader("input.pdf")
69
writer = PdfWriter()
70
71
page = reader.pages[0]
72
page.rotate(90) # Rotate 90 degrees clockwise
73
writer.add_page(page)
74
75
with open("rotated.pdf", "wb") as output:
76
writer.write(output)
77
```
78
79
### pdfplumber - Text and Table Extraction
80
81
#### Extract Text with Layout
82
```python
83
import pdfplumber
84
85
with pdfplumber.open("document.pdf") as pdf:
86
for page in pdf.pages:
87
text = page.extract_text()
88
print(text)
89
```
90
91
#### Extract Tables
92
```python
93
with pdfplumber.open("document.pdf") as pdf:
94
for i, page in enumerate(pdf.pages):
95
tables = page.extract_tables()
96
for j, table in enumerate(tables):
97
print(f"Table {j+1} on page {i+1}:")
98
for row in table:
99
print(row)
100
```
101
102
#### Advanced Table Extraction
103
```python
104
import pandas as pd
105
106
with pdfplumber.open("document.pdf") as pdf:
107
all_tables = []
108
for page in pdf.pages:
109
tables = page.extract_tables()
110
for table in tables:
111
if table: # Check if table is not empty
112
df = pd.DataFrame(table[1:], columns=table[0])
113
all_tables.append(df)
114
115
# Combine all tables
116
if all_tables:
117
combined_df = pd.concat(all_tables, ignore_index=True)
118
combined_df.to_excel("extracted_tables.xlsx", index=False)
119
```
120
121
### reportlab - Create PDFs
122
123
#### Basic PDF Creation
124
```python
125
from reportlab.lib.pagesizes import letter
126
from reportlab.pdfgen import canvas
127
128
c = canvas.Canvas("hello.pdf", pagesize=letter)
129
width, height = letter
130
131
# Add text
132
c.drawString(100, height - 100, "Hello World!")
133
c.drawString(100, height - 120, "This is a PDF created with reportlab")
134
135
# Add a line
136
c.line(100, height - 140, 400, height - 140)
137
138
# Save
139
c.save()
140
```
141
142
#### Create PDF with Multiple Pages
143
```python
144
from reportlab.lib.pagesizes import letter
145
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
146
from reportlab.lib.styles import getSampleStyleSheet
147
148
doc = SimpleDocTemplate("report.pdf", pagesize=letter)
149
styles = getSampleStyleSheet()
150
story = []
151
152
# Add content
153
title = Paragraph("Report Title", styles['Title'])
154
story.append(title)
155
story.append(Spacer(1, 12))
156
157
body = Paragraph("This is the body of the report. " * 20, styles['Normal'])
158
story.append(body)
159
story.append(PageBreak())
160
161
# Page 2
162
story.append(Paragraph("Page 2", styles['Heading1']))
163
story.append(Paragraph("Content for page 2", styles['Normal']))
164
165
# Build PDF
166
doc.build(story)
167
```
168
169
#### Subscripts and Superscripts
170
171
**IMPORTANT**: Never use Unicode subscript/superscript characters (₀₁₂₃₄₅₆₇₈₉, ⁰¹²³⁴⁵⁶⁷⁸⁹) in ReportLab PDFs. The built-in fonts do not include these glyphs, causing them to render as solid black boxes.
172
173
Instead, use ReportLab's XML markup tags in Paragraph objects:
174
```python
175
from reportlab.platypus import Paragraph
176
from reportlab.lib.styles import getSampleStyleSheet
177
178
styles = getSampleStyleSheet()
179
180
# Subscripts: use <sub> tag
181
chemical = Paragraph("H<sub>2</sub>O", styles['Normal'])
182
183
# Superscripts: use <super> tag
184
squared = Paragraph("x<super>2</super> + y<super>2</super>", styles['Normal'])
185
```
186
187
For canvas-drawn text (not Paragraph objects), manually adjust font the size and position rather than using Unicode subscripts/superscripts.
188
189
## Command-Line Tools
190
191
### pdftotext (poppler-utils)
192
```bash
193
# Extract text
194
pdftotext input.pdf output.txt
195
196
# Extract text preserving layout
197
pdftotext -layout input.pdf output.txt
198
199
# Extract specific pages
200
pdftotext -f 1 -l 5 input.pdf output.txt # Pages 1-5
201
```
202
203
### qpdf
204
```bash
205
# Merge PDFs
206
qpdf --empty --pages file1.pdf file2.pdf -- merged.pdf
207
208
# Split pages
209
qpdf input.pdf --pages . 1-5 -- pages1-5.pdf
210
qpdf input.pdf --pages . 6-10 -- pages6-10.pdf
211
212
# Rotate pages
213
qpdf input.pdf output.pdf --rotate=+90:1 # Rotate page 1 by 90 degrees
214
215
# Remove password
216
qpdf --password=mypassword --decrypt encrypted.pdf decrypted.pdf
217
```
218
219
### pdftk (if available)
220
```bash
221
# Merge
222
pdftk file1.pdf file2.pdf cat output merged.pdf
223
224
# Split
225
pdftk input.pdf burst
226
227
# Rotate
228
pdftk input.pdf rotate 1east output rotated.pdf
229
```
230
231
## Common Tasks
232
233
### Extract Text from Scanned PDFs
234
```python
235
# Requires: pip install pytesseract pdf2image
236
import pytesseract
237
from pdf2image import convert_from_path
238
239
# Convert PDF to images
240
images = convert_from_path('scanned.pdf')
241
242
# OCR each page
243
text = ""
244
for i, image in enumerate(images):
245
text += f"Page {i+1}:\n"
246
text += pytesseract.image_to_string(image)
247
text += "\n\n"
248
249
print(text)
250
```
251
252
### Add Watermark
253
```python
254
from pypdf import PdfReader, PdfWriter
255
256
# Create watermark (or load existing)
257
watermark = PdfReader("watermark.pdf").pages[0]
258
259
# Apply to all pages
260
reader = PdfReader("document.pdf")
261
writer = PdfWriter()
262
263
for page in reader.pages:
264
page.merge_page(watermark)
265
writer.add_page(page)
266
267
with open("watermarked.pdf", "wb") as output:
268
writer.write(output)
269
```
270
271
### Extract Images
272
```bash
273
# Using pdfimages (poppler-utils)
274
pdfimages -j input.pdf output_prefix
275
276
# This extracts all images as output_prefix-000.jpg, output_prefix-001.jpg, etc.
277
```
278
279
### Password Protection
280
```python
281
from pypdf import PdfReader, PdfWriter
282
283
reader = PdfReader("input.pdf")
284
writer = PdfWriter()
285
286
for page in reader.pages:
287
writer.add_page(page)
288
289
# Add password
290
writer.encrypt("userpassword", "ownerpassword")
291
292
with open("encrypted.pdf", "wb") as output:
293
writer.write(output)
294
```
295
296
## Quick Reference
297
298
| Task | Best Tool | Command/Code |
299
|------|-----------|--------------|
300
| Merge PDFs | pypdf | `writer.add_page(page)` |
301
| Split PDFs | pypdf | One page per file |
302
| Extract text | pdfplumber | `page.extract_text()` |
303
| Extract tables | pdfplumber | `page.extract_tables()` |
304
| Create PDFs | reportlab | Canvas or Platypus |
305
| Command line merge | qpdf | `qpdf --empty --pages ...` |
306
| OCR scanned PDFs | pytesseract | Convert to image first |
307
| Fill PDF forms | pdf-lib or pypdf (see FORMS.md) | See FORMS.md |
308
309
## Next Steps
310
311
- For advanced pypdfium2 usage, see REFERENCE.md
312
- For JavaScript libraries (pdf-lib), see REFERENCE.md
313
- If you need to fill out a PDF form, follow the instructions in FORMS.md
314
- For troubleshooting guides, see REFERENCE.md
315

© Anthropic, PBC · Apache 2.0 · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 6,373 prompts

More by @OpenSourceBuffGuy

See all
Website Clone Architect prompt image

Website Clone Architect

Reverse-engineers a website section by section, writes component specs, and hands them to parallel builder agents in git worktrees to rebuild it.

agent-skill, orchestrator, parallel-agents, multi-agent, plan-then-execute, web-development, frontend, codebase-analysis, scaffolding, css, html, nextjs, typescript, git, mcp, claude-code, for-developers, for-designers, long-form, advanced, technical-spec, task-decomposition, structured-output, design-systems, ui-ux, tool-use, checklist, report, vibe-coding

Italian Terrace Cafe Candid Portrait prompt image

Italian Terrace Cafe Candid Portrait

Generates a photorealistic candid shot of a woman in a white corset dress laughing at a sunny hotel terrace cafe, written as detailed JSON.

image-prompt, json-prompt, structured-output, photorealistic, portrait, woman, candid, fashion, cottagecore, natural-light, golden-hour, bokeh, summer, travel, cityscape, mountains, medium-shot, vibrant-colors, for-photographers, nano-banana, fashion-shoot, luxury

Night Street Photo With Leashed Cat prompt image

Night Street Photo With Leashed Cat

Generates a flash-lit night street photo of a blonde woman in a retro track jacket walking a hissing tabby cat on a leash.

image-prompt, json-prompt, nano-banana, structured-output, portrait, woman, animals, pet-portrait, night-scene, street-photography, candid, editorial, fashion, dramatic-lighting, photorealistic, vertical-format, medium-shot, graffiti, for-photographers, for-content-creators

1 view

Reviews

Loading reviews…

Discussion

Loading comments…