forked from pymupdf/PyMuPDF-Utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
all-my-pics-embedded.py
74 lines (58 loc) · 1.7 KB
/
all-my-pics-embedded.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""
@created: 2018-09-02 18:00:00
@author: (c) 2018 Jorj X. McKie
Embed all files from a directory
-------------------------------------------------------------------------------
Dependencies:
-------------
PyMuPDF
PySimpleGUI, optional: requires Python 3 if used
License:
--------
GNU GPL V3+
Description
------------
Take all files from a directory and embed them in a new PDF.
"""
from __future__ import print_function
import os, time, sys, fitz
print(fitz.__doc__)
# do some adjustments whether Python v2 or v3
if str is not bytes:
import PySimpleGUI as psg
mytime = time.perf_counter
else:
mytime = time.clock
rc = False
if str is bytes:
imgdir = sys.argv[1] # where my files are
else:
imgdir = psg.PopupGetFolder(
"Make a PDF from Embedded Files", "Enter file directory:"
)
if not imgdir:
raise SystemExit()
t0 = mytime() # set start timer
doc = fitz.open()
width, height = fitz.PaperSize("a4")
rect = fitz.Rect(0, 0, width, height) + (36, 36, -36, -36)
imglist = os.listdir(imgdir)
imgcount = len(imglist)
for i, f in enumerate(imglist):
path = os.path.join(imgdir, f)
if not os.path.isfile(path):
print("skipping non-file '%s'!" % f)
continue
if str is not bytes:
psg.OneLineProgressMeter(
"Embedding Files", i + 1, imgcount, "dir: " + imgdir, "file: " + f
)
else:
print("embedding file '%s', (%i / %i)" % (f, i + 1, imgcount))
img = open(path, "rb").read()
doc.embeddedFileAdd(f, img, filename=f, ufilename=f, desc=f)
page = doc.newPage() # every doc needs at least one page
doc.save("all-my-pics-embedded.pdf")
t1 = mytime()
print("%g" % round(t1 - t0, 3), "sec processing time")