Skip to content

Commit a12ee39

Browse files
committed
Improve syntax for Slice file action
Improve the syntax for Slice file action to allow the user to omit the second value of a page range in order to indicate that it ends on the last page of the PDF file (eg. "8-").
1 parent 378619e commit a12ee39

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

Alfred PDF Tools.alfredworkflow

20 Bytes
Binary file not shown.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Download the workflow file from [GitHub releases][2] and install it by double-cl
2222
* `Merge`: Merge the selected PDF files. Use the `` modifier key if you also want to move the source files to Trash;
2323
* `Split by Page Count`: Split the selected PDF file by page count.
2424
* `Split by File Size`: Split the selected PDF file by file size.
25-
* `Slice in Multiple Files`: Slice the selected PDF file in multiple files by entering page numbers and/or page ranges separated by commas (e.g. 2, 5-8).
26-
* `Slice in a Single File`: Slice the selected PDF file in a single file by entering page numbers and/or page ranges separated by commas (e.g. 2, 5-8).
25+
* `Slice in Multiple Files`: Slice the selected PDF file in multiple files by entering page numbers and/or page ranges separated by commas (e.g. 2, 5-8, 20-).
26+
* `Slice in a Single File`: Slice the selected PDF file in a single file by entering page numbers and/or page ranges separated by commas (e.g. 2, 5-8, 20-).
2727
* `Crop`: Convert two-column pages in single pages.
2828
* `Scale`: Scale the selected PDF files to a given paper size.
2929

src/alfred_pdf_tools.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ def split_size(query, abs_path, suffix):
404404
for i in xrange(pg_cnt):
405405
writer.addPage(reader.getPage(i))
406406

407-
writer.removeLinks()
408407
inp_file = tempfile.NamedTemporaryFile()
409408
writer.write(inp_file)
410409

@@ -543,26 +542,21 @@ def slice_(query, abs_path, single, suffix):
543542
pages = [x.strip() for x in query.split(',')]
544543

545544
for page in pages:
546-
if page.replace('-', '').isdigit():
547-
pass
548-
549-
else:
545+
if not page.replace('-', '').isdigit():
550546
raise SyntaxError
551547

552548
for page in pages:
553549
if "-" in page:
554-
stop = int(page.split('-')[1])
555-
if stop > reader.numPages:
556-
raise IndexError
550+
if page.split('-')[1]:
551+
stop = int(page.split('-')[1])
557552
else:
558-
pass
553+
stop = reader.numPages
559554

560555
else:
561556
stop = int(page)
562-
if stop > reader.numPages:
563-
raise IndexError
564-
else:
565-
pass
557+
558+
if stop > reader.numPages:
559+
raise IndexError
566560

567561
noextpath = os.path.splitext(abs_path)[0]
568562

@@ -572,36 +566,41 @@ def slice_(query, abs_path, single, suffix):
572566
for page in pages:
573567
if "-" in page:
574568
start = int(page.split('-')[0]) - 1
575-
stop = int(page.split('-')[1])
569+
stop_str = page.split('-')[1]
570+
571+
if stop_str:
572+
stop = int(stop_str)
573+
else:
574+
stop = reader.numPages
576575

577576
if start == -1:
578577
raise StartValueZeroError
579578

580579
if start >= stop:
581580
raise StartValueReverseError
582581

583-
merger.append(reader, pages=(start, stop))
584-
585582
else:
586583
start = int(page) - 1
587584
stop = int(page)
588-
merger.append(reader, pages=(start, stop))
589585

590-
merger.write(noextpath + ' (slice).pdf')
586+
merger.append(reader, pages=(start, stop))
587+
588+
merger.write(noextpath + ' (sliced).pdf')
591589

592590
else:
593591
part_no = 0
594592

595593
for page in pages:
596-
597-
out_file = '{} ({} {}).pdf'.format(noextpath,
598-
suffix,
599-
part_no + 1)
594+
merger = PdfFileMerger(strict=False)
600595

601596
if "-" in page:
602-
merger = PdfFileMerger(strict=False)
603597
start = int(page.split('-')[0]) - 1
604-
stop = int(page.split('-')[1])
598+
stop_str = page.split('-')[1]
599+
600+
if stop_str:
601+
stop = int(stop_str)
602+
else:
603+
stop = reader.numPages
605604

606605
if start == -1:
607606
raise StartValueZeroError
@@ -610,10 +609,12 @@ def slice_(query, abs_path, single, suffix):
610609
raise StartValueReverseError
611610

612611
else:
613-
merger = PdfFileMerger(strict=False)
614612
start = int(page) - 1
615613
stop = int(page)
616614

615+
out_file = '{} ({} {}).pdf'.format(noextpath,
616+
suffix,
617+
part_no + 1)
617618
merger.append(reader, pages=(start, stop))
618619
merger.write(out_file)
619620
part_no += 1

src/info.plist

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,9 @@ python alfred_pdf_tools.py --progress "{query}"</string>
10011001
<key>argumenttype</key>
10021002
<integer>0</integer>
10031003
<key>subtext</key>
1004-
<string></string>
1004+
<string>e.g. "2, 5-8, 20-"</string>
10051005
<key>text</key>
1006-
<string>Enter page numbers and/or page ranges (e.g. 2, 5-8).</string>
1006+
<string>Enter page numbers and/or page ranges.</string>
10071007
<key>withspace</key>
10081008
<true/>
10091009
</dict>
@@ -1080,9 +1080,9 @@ python alfred_pdf_tools.py --progress "{query}"</string>
10801080
<key>argumenttype</key>
10811081
<integer>0</integer>
10821082
<key>subtext</key>
1083-
<string></string>
1083+
<string>e.g. "2, 5-8, 20-"</string>
10841084
<key>text</key>
1085-
<string>Enter page numbers and/or page ranges (e.g. 2, 5-8).</string>
1085+
<string>Enter page numbers and/or page ranges.</string>
10861086
<key>withspace</key>
10871087
<true/>
10881088
</dict>
@@ -1299,8 +1299,8 @@ Download the workflow file from [GitHub releases][2] and install it by double-cl
12991299
* `Merge`: Merge the selected PDF files. Use the `⌘` modifier key if you also want to move the source files to Trash;
13001300
* `Split by Page Count`: Split the selected PDF file by page count.
13011301
* `Split by File Size`: Split the selected PDF file by file size.
1302-
* `Slice in Multiple Files`: Slice the selected PDF file in multiple files by entering page numbers and/or page ranges separated by commas (e.g. 2, 5-8).
1303-
* `Slice in a Single File`: Slice the selected PDF file in a single file by entering page numbers and/or page ranges separated by commas (e.g. 2, 5-8).
1302+
* `Slice in Multiple Files`: Slice the selected PDF file in multiple files by entering page numbers and/or page ranges separated by commas (e.g. 2, 5-8, 20-).
1303+
* `Slice in a Single File`: Slice the selected PDF file in a single file by entering page numbers and/or page ranges separated by commas (e.g. 2, 5-8, 20-).
13041304
* `Crop`: Convert two-column pages in single pages.
13051305
* `Scale`: Scale the selected PDF files to a given paper size.
13061306
@@ -1727,7 +1727,7 @@ This workflow relies on [PyPDF2][5] library currently maintained by [Phaseit, In
17271727
<string>part</string>
17281728
</dict>
17291729
<key>version</key>
1730-
<string>2.13</string>
1730+
<string>2.14</string>
17311731
<key>webaddress</key>
17321732
<string>https://github.com/xilopaint/alfred-pdf-tools</string>
17331733
</dict>

0 commit comments

Comments
 (0)