Skip to content

Commit a0355cf

Browse files
committed
add simple command line to convert images to pdf
1 parent 9412326 commit a0355cf

File tree

11 files changed

+112
-2
lines changed

11 files changed

+112
-2
lines changed

_unittests/ut_tools/data/image.png

2.14 KB
Loading

_unittests/ut_tools/data/img4.png

5.28 KB
Loading
347 KB
Loading
231 KB
Loading
811 KB
Binary file not shown.
811 KB
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import unittest
3+
from sphinx_runpython.ext_test_case import ExtTestCase
4+
from sphinx_runpython.tools.img_export import images2pdf
5+
6+
7+
class TestImgExport(ExtTestCase):
8+
9+
def test_export1(self):
10+
dest = "test_export1.pdf"
11+
data = os.path.join(os.path.dirname(__file__), "data", "mazures1.jpg")
12+
datap = os.path.join(os.path.dirname(__file__), "data", "*.jpg")
13+
res = images2pdf([data, datap], dest)
14+
self.assertExists(dest)
15+
self.assertEqual(len(res), 3)
16+
17+
def test_export2(self):
18+
dest = "test_export2.pdf"
19+
data = os.path.join(os.path.dirname(__file__), "data", "mazures1.jpg")
20+
datap = os.path.join(os.path.dirname(__file__), "data", "*.jpg")
21+
res = images2pdf(",".join([data, datap]), dest)
22+
self.assertExists(dest)
23+
self.assertEqual(len(res), 3)
24+
25+
26+
if __name__ == "__main__":
27+
unittest.main(verbosity=2)

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ black
22
coverage
33
flake8
44
furo
5+
img2pdf
56
isort
67
matplotlib
78
numpydoc

sphinx_runpython/_cmd_helper.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
from argparse import ArgumentParser
44
from tempfile import TemporaryDirectory
5-
from .convert import convert_ipynb_to_gallery
65

76

87
def get_parser():
@@ -12,7 +11,8 @@ def get_parser():
1211
epilog="",
1312
)
1413
parser.add_argument(
15-
"command", help="Command to run, only 'nb2py' or 'readme' are available"
14+
"command",
15+
help="Command to run, only 'nb2py', 'readme', 'img2pdf' are available",
1616
)
1717
parser.add_argument(
1818
"-p", "--path", help="Folder or file which contains the files to process"
@@ -23,11 +23,18 @@ def get_parser():
2323
help="Recursive search.",
2424
action="store_true",
2525
)
26+
parser.add_argument(
27+
"-o",
28+
"--output",
29+
help="output",
30+
)
2631
parser.add_argument("-v", "--verbose", help="verbosity", default=1, type=int)
2732
return parser
2833

2934

3035
def nb2py(infolder: str, recursive: bool = False, verbose: int = 0):
36+
from .convert import convert_ipynb_to_gallery
37+
3138
if not os.path.exists(infolder):
3239
raise FileNotFoundError(f"Unable to find {infolder!r}.")
3340
patterns = [infolder + "/*.ipynb", infolder + "/**/*.ipynb"]
@@ -47,6 +54,11 @@ def process_args(args):
4754
if cmd == "nb2py":
4855
nb2py(args.path, recursive=args.recursive, verbose=args.verbose)
4956
return
57+
if cmd == "img2pdf":
58+
from .tools.img_export import images2pdf
59+
60+
images2pdf(args.path, args.output, verbose=args.verbose)
61+
return
5062
if cmd == "readme":
5163
from .readme import check_readme_syntax
5264

sphinx_runpython/tools/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)