This repository was archived by the owner on May 19, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +23
-11
lines changed Expand file tree Collapse file tree 2 files changed +23
-11
lines changed Original file line number Diff line number Diff line change 1
1
from dataclasses import dataclass
2
2
from typing import List , Tuple
3
+ from pathlib import Path
4
+ import re
3
5
4
6
from PyPDF2 import PdfFileReader , PdfFileWriter
5
7
8
+ class InputFile :
9
+ def __init__ (self , rawpath : str ) -> None :
10
+ self .path = Path (rawpath )
11
+ self .abspath = self .path .as_posix ()
12
+ self .name = self .path .name
13
+ self .id = re .match (r'^\d+' , self .name ).group ()
14
+
6
15
@dataclass
7
16
class SplitFile :
8
- filename : str
17
+ name : str
9
18
pages : Tuple [int , int ]
10
19
11
- def get_range (self ) -> range :
20
+ def get_pages_range (self ) -> range :
12
21
return range (self .pages [0 ], self .pages [1 ] + 1 )
13
22
14
- def split_pdf (filename : str , splits : List [SplitFile ]) -> None :
15
- original = PdfFileReader (filename )
23
+ def split_pdf (inputfile : InputFile , splits : List [SplitFile ]) -> None :
24
+ original = PdfFileReader (inputfile . abspath )
16
25
for split in splits :
17
26
output = PdfFileWriter ()
18
- for page_number in split .get_range ():
27
+ for page_number in split .get_pages_range ():
19
28
output .addPage (original .getPage (page_number ))
20
- with open (f'{ split . filename } .pdf ' , 'wb' ) as file :
29
+ with open (f'{ inputfile . id } - { split . name } ' , 'wb' ) as file :
21
30
output .write (file )
Original file line number Diff line number Diff line change 1
1
import argparse
2
2
3
- from editor import SplitFile , split_pdf
3
+ from editor import InputFile , SplitFile , split_pdf
4
4
5
5
parser = argparse .ArgumentParser ()
6
6
parser .add_argument (
7
- 'filename ' ,
7
+ 'filepath ' ,
8
8
type = str ,
9
- help = 'name of the input PDF file to split'
9
+ help = "absolute path of the input PDF file to split, or just its name if it's in the same folder"
10
10
)
11
11
parser .add_argument (
12
12
'-s' , '--split-name' ,
13
13
action = 'append' ,
14
+ required = True ,
14
15
help = "name of a split file, it's not needed to include the extension (.pdf)"
15
16
)
16
17
parser .add_argument (
17
18
'-p' , '--page-numbers' ,
18
19
action = 'append' ,
20
+ required = True ,
19
21
type = int ,
20
22
nargs = 2 ,
21
23
help = 'first and last page numbers to split from the input file'
26
28
if len (args .page_numbers ) != len (args .split_name ):
27
29
raise argparse .ArgumentTypeError ('specify the same number of page ranges as the number of splits.' )
28
30
29
- splits = [SplitFile (name , pages ) for name , pages in zip (args .split_name , args .page_numbers )]
30
- split_pdf (args .filename , splits )
31
+ inputfile = InputFile (args .filepath )
32
+ splits = [SplitFile (f'{ name } .pdf' , pages ) for name , pages in zip (args .split_name , args .page_numbers )]
33
+ split_pdf (inputfile , splits )
You can’t perform that action at this time.
0 commit comments