-
Notifications
You must be signed in to change notification settings - Fork 140
/
export.py
36 lines (28 loc) · 898 Bytes
/
export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Export an embedded file from the input document to the output document
-------------------------------------------------------------------------------
License: GNU AGPL V3
(c) 2021 Jorj X. McKie
Usage
-----
python export.py input.pdf joe-caione-qO-PIF84Vxg-unsplash.jpg output.pdf
Notes
-----
The output.pdf file generated in examples/embed-images is renamed as input.pdf
to be used as the input file in this example.
Dependencies
------------
PyMuPDF
"""
from __future__ import print_function
import sys
import fitz
pdffn = sys.argv[1] # PDF file name
name = sys.argv[2] # embedded file identifier
expfn = sys.argv[3] # filename of exported file
doc = fitz.open(pdffn) # open PDF
outfile = open(expfn, "wb") # to be on the safe side always open binary
# extract file content. Will get exception on any error.
content = doc.embfile_get(name)
outfile.write(content)
outfile.close()