Skip to content

Commit 964fb47

Browse files
authored
ENH: Add example with attachment (#26)
1 parent d87c33e commit 964fb47

File tree

8 files changed

+51
-18
lines changed

8 files changed

+51
-18
lines changed

.github/workflows/ci.txt

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#
2-
# This file is autogenerated by pip-compile with python 3.11
3-
# To update, run:
2+
# This file is autogenerated by pip-compile with Python 3.11
3+
# by the following command:
44
#
55
# pip-compile .github/workflows/ci.in
66
#
7-
pydantic==1.10.4
8-
# via -r .github/workflows/ci.in
9-
pypdf==3.3.0
7+
annotated-types==0.6.0
8+
# via pydantic
9+
pydantic==2.5.0
1010
# via -r .github/workflows/ci.in
11-
typing-extensions==4.4.0
11+
pydantic-core==2.14.1
1212
# via pydantic
13+
pypdf==3.17.0
14+
# via -r .github/workflows/ci.in
15+
typing-extensions==4.8.0
16+
# via
17+
# pydantic
18+
# pydantic-core

.github/workflows/json_consistency.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import datetime
22
import json
3+
import logging
34
import sys
45
from pathlib import Path
56

67
from pydantic import BaseModel, NonNegativeInt
78

89
from pypdf import PdfReader
910

11+
logger = logging.getLogger()
12+
13+
logger.level = logging.ERROR
14+
1015

1116
class AnnotationCount(BaseModel):
12-
Highlight: int | None
13-
Ink: int | None
14-
Link: int | None
15-
Text: int | None
16-
Widget: int | None
17+
Highlight: int | None = None
18+
Ink: int | None = None
19+
Link: int | None = None
20+
Text: int | None = None
21+
Widget: int | None = None
1722

1823
def items(self) -> list[tuple[str, int]]:
1924
return [
@@ -47,7 +52,7 @@ def main() -> None:
4752
"""Check the consistency of the JSON metadata file."""
4853
with open("files.json") as f:
4954
data = json.load(f)
50-
main_pdf = MainPdfFile.parse_obj(data)
55+
main_pdf = MainPdfFile.model_validate(data)
5156
registered_pdfs = []
5257

5358
seen_failure = False

.pre-commit-config.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# pre-commit run --all-files
22
repos:
33
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v4.1.0
4+
rev: v4.5.0
55
hooks:
66
- id: check-ast
77
- id: check-case-conflict
@@ -20,21 +20,21 @@ repos:
2020
hooks:
2121
- id: isort
2222
- repo: https://github.com/psf/black
23-
rev: 22.1.0
23+
rev: 22.3.0
2424
hooks:
2525
- id: black
2626
- repo: https://github.com/asottile/pyupgrade
27-
rev: v2.31.1
27+
rev: v3.15.0
2828
hooks:
2929
- id: pyupgrade
3030
args: [--py39-plus]
3131
- repo: https://github.com/asottile/blacken-docs
32-
rev: v1.12.1
32+
rev: 1.16.0
3333
hooks:
3434
- id: blacken-docs
3535
additional_dependencies: [black==22.1.0]
3636
- repo: https://github.com/charliermarsh/ruff-pre-commit
37-
rev: 'v0.0.280'
37+
rev: 'v0.1.5'
3838
hooks:
3939
- id: ruff
4040
args: ['--fix']

025-attachment/add_attachment.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from pypdf import PdfReader, PdfWriter
2+
3+
reader = PdfReader("../001-trivial/minimal-document.pdf")
4+
writer = PdfWriter()
5+
writer.append_pages_from_reader(reader)
6+
7+
with open("image.png", "rb") as file:
8+
writer.add_attachment("image.png", file.read())
9+
with open("with-attachment.pdf", "wb") as file:
10+
writer.write(file)

025-attachment/image.png

6.51 KB
Loading

025-attachment/with-attachment.pdf

23.7 KB
Binary file not shown.

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
maint:
2+
pip-compile .github/workflows/ci.in --upgrade

files.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@
317317
"Highlight": 1,
318318
"Ink": 1
319319
}
320+
},
321+
{
322+
"path": "025-attachment/with-attachment.pdf",
323+
"producer": "pypdf",
324+
"creation_date": null,
325+
"encrypted": false,
326+
"pages": 1,
327+
"images": 1,
328+
"forms": 0,
329+
"annotations": {}
320330
}
321331
]
322-
}
332+
}

0 commit comments

Comments
 (0)