|
| 1 | +import argparse |
| 2 | +import calendar |
| 3 | +import os |
| 4 | +import sys |
| 5 | + |
| 6 | +import img2pdf |
| 7 | +from PyPDF2.pdf import PdfFileWriter, PdfFileReader |
| 8 | +import time |
| 9 | + |
| 10 | +# Get the timestamp |
| 11 | +ts = calendar.timegm(time.gmtime()) |
| 12 | + |
| 13 | + |
| 14 | +def PdfPassword(filepath, password): |
| 15 | + # Check if file exists |
| 16 | + checkFile = os.path.isfile(filepath) |
| 17 | + |
| 18 | + if checkFile: |
| 19 | + # Get the path of directory and filename |
| 20 | + path, filename = os.path.split(filepath) |
| 21 | + |
| 22 | + # Get the file extension to check for pdf files |
| 23 | + file_extension = os.path.splitext(filepath)[1] |
| 24 | + |
| 25 | + if file_extension == ".pdf": |
| 26 | + |
| 27 | + # The output filename |
| 28 | + output_file = os.path.join(path, f"temp_{ts}_{filename}") |
| 29 | + |
| 30 | + # Create a PdfFileWriter object |
| 31 | + pdf_writer = PdfFileWriter() |
| 32 | + |
| 33 | + # Open our PDF file with the PdfFileReader |
| 34 | + file = PdfFileReader(filepath) |
| 35 | + |
| 36 | + # Get number of pages in original file |
| 37 | + # Iterate through every page of the original file and add it to our new file |
| 38 | + for idx in range(file.numPages): |
| 39 | + # Get the page at index idx |
| 40 | + page = file.getPage(idx) |
| 41 | + |
| 42 | + # Add it to the output file |
| 43 | + pdf_writer.addPage(page) |
| 44 | + |
| 45 | + # Encrypt the new file with the entered password |
| 46 | + pdf_writer.encrypt(password, use_128bit=True) |
| 47 | + |
| 48 | + # Open a new file |
| 49 | + with open(output_file, "wb") as file: |
| 50 | + # Write our encrypted PDF to this file |
| 51 | + pdf_writer.write(file) |
| 52 | + |
| 53 | + print('File Written To Path:', output_file) |
| 54 | + |
| 55 | + else: |
| 56 | + # File extension is not PDF |
| 57 | + print(f"Not A PDF File Given, File Has Extension: {file_extension}") |
| 58 | + sys.exit() |
| 59 | + |
| 60 | + else: |
| 61 | + # No file exists on the current path |
| 62 | + print("Check The File Path") |
| 63 | + sys.exit() |
| 64 | + |
| 65 | + |
| 66 | +def PdfMultiplePassword(filepaths, password): |
| 67 | + # Check if files exists |
| 68 | + check_path = [os.path.isfile(x) for x in filepaths] |
| 69 | + |
| 70 | + # Gets the files extension |
| 71 | + file_extensions = [os.path.splitext(x)[1] for x in filepaths] |
| 72 | + |
| 73 | + # Check if files extension are pdf |
| 74 | + file_extensions_check = [x for x in file_extensions if x != ".pdf"] |
| 75 | + |
| 76 | + if False in check_path: |
| 77 | + |
| 78 | + # Get the index of the file that doesn't exists |
| 79 | + index = check_path.index(False) |
| 80 | + print(f"File Doesn't Exists: {filepaths[index]}") |
| 81 | + sys.exit() |
| 82 | + |
| 83 | + else: |
| 84 | + # Not a PDF file is given |
| 85 | + if file_extensions_check: |
| 86 | + print("Submit Only PDF Files") |
| 87 | + sys.exit() |
| 88 | + |
| 89 | + else: |
| 90 | + count = 1 |
| 91 | + # Iterate through every pdf of the filepaths |
| 92 | + for path in filepaths: |
| 93 | + |
| 94 | + # Create a PdfFileWriter object |
| 95 | + pdf_writer = PdfFileWriter() |
| 96 | + |
| 97 | + # Open our PDF file with the PdfFileReader |
| 98 | + pdf_reader = PdfFileReader(path) |
| 99 | + |
| 100 | + # Get the page at index idx |
| 101 | + for page in range(pdf_reader.getNumPages()): |
| 102 | + # Add each page to the writer object |
| 103 | + pdf_writer.addPage(pdf_reader.getPage(page)) |
| 104 | + |
| 105 | + # The output filename |
| 106 | + output_file = f"merge_enc_{count}_{ts}.pdf" |
| 107 | + |
| 108 | + # Encrypt the new file with the entered password |
| 109 | + pdf_writer.encrypt(password, use_128bit=True) |
| 110 | + |
| 111 | + # Write out the merged PDF |
| 112 | + with open(output_file, 'wb') as file: |
| 113 | + pdf_writer.write(file) |
| 114 | + |
| 115 | + count += 1 |
| 116 | + print('File Written To Path:', output_file) |
| 117 | + |
| 118 | + |
| 119 | +def PdfMerge(filepaths, password=None): |
| 120 | + # Check if files exists |
| 121 | + check_path = [os.path.isfile(x) for x in filepaths] |
| 122 | + |
| 123 | + # Gets the files extension |
| 124 | + file_extensions = [os.path.splitext(x)[1] for x in filepaths] |
| 125 | + |
| 126 | + # Check if files extension are pdf |
| 127 | + file_extensions_check = [x for x in file_extensions if x != ".pdf"] |
| 128 | + |
| 129 | + if False in check_path: |
| 130 | + |
| 131 | + # Get the index of the file that doesn't exists |
| 132 | + index = check_path.index(False) |
| 133 | + print(f"File Doesn't Exists: {filepaths[index]}") |
| 134 | + sys.exit() |
| 135 | + |
| 136 | + else: |
| 137 | + # Not a PDF file is given |
| 138 | + if file_extensions_check: |
| 139 | + print("Submit Only PDF Files") |
| 140 | + sys.exit() |
| 141 | + |
| 142 | + else: |
| 143 | + |
| 144 | + # Create a PdfFileWriter object |
| 145 | + pdf_writer = PdfFileWriter() |
| 146 | + |
| 147 | + # Iterate through every pdf of the filepaths |
| 148 | + for path in filepaths: |
| 149 | + |
| 150 | + # Open our PDF file with the PdfFileReader |
| 151 | + pdf_reader = PdfFileReader(path) |
| 152 | + |
| 153 | + # Get the page at index idx |
| 154 | + for page in range(pdf_reader.getNumPages()): |
| 155 | + # Add each page to the writer object |
| 156 | + pdf_writer.addPage(pdf_reader.getPage(page)) |
| 157 | + |
| 158 | + if password: |
| 159 | + # The output filename |
| 160 | + output_file = f"merge_enc_{ts}.pdf" |
| 161 | + |
| 162 | + # Encrypt the new file with the entered password |
| 163 | + pdf_writer.encrypt(password, use_128bit=True) |
| 164 | + |
| 165 | + # Write out the merged PDF |
| 166 | + with open(output_file, 'wb') as file: |
| 167 | + pdf_writer.write(file) |
| 168 | + |
| 169 | + print('File Written To Path:', output_file) |
| 170 | + |
| 171 | + else: |
| 172 | + # The output filename |
| 173 | + output_file = f"merge_{ts}.pdf" |
| 174 | + |
| 175 | + # Write out the merged PDF |
| 176 | + with open(output_file, 'wb') as file: |
| 177 | + pdf_writer.write(file) |
| 178 | + |
| 179 | + print('File Written To Path:', output_file) |
| 180 | + |
| 181 | + |
| 182 | +def PdfImage(filepaths, merger=False): |
| 183 | + # Check if files exists |
| 184 | + check_path = [os.path.isfile(x) for x in filepaths] |
| 185 | + |
| 186 | + if False in check_path: |
| 187 | + # Get the index of the file that doesn't exists |
| 188 | + index = check_path.index(False) |
| 189 | + print(f"File Doesn't Exists: {filepaths[index]}") |
| 190 | + sys.exit() |
| 191 | + |
| 192 | + else: |
| 193 | + # Check if the merger is True |
| 194 | + if merger: |
| 195 | + try: |
| 196 | + # Write the file with the name as name.pdf |
| 197 | + with open("name.pdf", "wb") as f: |
| 198 | + f.write(img2pdf.convert(filepaths)) |
| 199 | + # Exception if the the image can't be opened |
| 200 | + except img2pdf.ImageOpenError: |
| 201 | + print("Cannot Read Image") |
| 202 | + sys.exit() |
| 203 | + else: |
| 204 | + # Output filename |
| 205 | + output_file = f"img_{ts}.pdf" |
| 206 | + try: |
| 207 | + # Write the file |
| 208 | + with open(output_file, "wb") as f: |
| 209 | + f.write(img2pdf.convert(filepaths)) |
| 210 | + # Exception if the the image can't be opened |
| 211 | + except img2pdf.ImageOpenError: |
| 212 | + print("Cannot Read Image") |
| 213 | + sys.exit() |
| 214 | + |
| 215 | + |
| 216 | +def PdfMultipleImage(filepaths, password=None): |
| 217 | + # Check if files exists |
| 218 | + files = [] |
| 219 | + check_path = [os.path.isfile(x) for x in filepaths] |
| 220 | + |
| 221 | + if False in check_path: |
| 222 | + # Get the index of the file that doesn't exists |
| 223 | + index = check_path.index(False) |
| 224 | + print(f"File Doesn't Exists: {filepaths[index]}") |
| 225 | + sys.exit() |
| 226 | + |
| 227 | + else: |
| 228 | + count = 1 |
| 229 | + # Iterate over the files |
| 230 | + for path in filepaths: |
| 231 | + # Name of the output file |
| 232 | + output_file = f"img_{count}_{ts}.pdf" |
| 233 | + try: |
| 234 | + # Convert the image to pdf and write the file |
| 235 | + with open(output_file, "wb") as f: |
| 236 | + f.write(img2pdf.convert(path)) |
| 237 | + # Check if password is provided |
| 238 | + if password: |
| 239 | + # Sends the file to the function to be encrypted |
| 240 | + PdfPassword(output_file, password) |
| 241 | + # Checks if the file exist |
| 242 | + if os.path.isfile(output_file): |
| 243 | + files.append(output_file) |
| 244 | + else: |
| 245 | + print('File Written To Path:', output_file) |
| 246 | + count += 1 |
| 247 | + # Exception if the the image can't be opened |
| 248 | + except img2pdf.ImageOpenError: |
| 249 | + print(f"Cannot Read Image: {path}") |
| 250 | + |
| 251 | + try: |
| 252 | + # Remove the unwanted files |
| 253 | + for rm in files: |
| 254 | + os.remove(rm) |
| 255 | + except: |
| 256 | + pass |
| 257 | + |
| 258 | + |
| 259 | +def main(): |
| 260 | + parser = argparse.ArgumentParser(description='MERGE, ENCRYPT PDF FILES. CONVERT IMAGES TO PDF,ENCRYPT THEM') |
| 261 | + |
| 262 | + # Argument is for a single filepath |
| 263 | + parser.add_argument('-f', dest='filepath', type=str, help='Path For Single PDF') |
| 264 | + |
| 265 | + # Argument is for a multiple filepaths |
| 266 | + parser.add_argument('-a', dest='filepaths', nargs='+', type=str, help='Paths For Multiple PDF') |
| 267 | + |
| 268 | + # Argument is used for to encrypt the file |
| 269 | + parser.add_argument('-e', dest='password', type=str, help='Password To Encrypt The PDF') |
| 270 | + |
| 271 | + # Argument is used to merge files |
| 272 | + parser.add_argument('-m', dest='merge', nargs='+', type=str, help='Merge Multiple PDF File') |
| 273 | + |
| 274 | + # Argument is used to convert images into single PDF file |
| 275 | + parser.add_argument('-i', dest='image', nargs='+', type=str, help='Convert Multiple Images To One PDF File') |
| 276 | + |
| 277 | + parser.add_argument('-s', dest='seprateimage', nargs='+', type=str, |
| 278 | + help='Convert Multiple Images To Separate PDF File') |
| 279 | + |
| 280 | + args = parser.parse_args() |
| 281 | + |
| 282 | + # Merge all the files and encrypt the file |
| 283 | + if args.filepath and args.merge and args.password: |
| 284 | + filename = args.filepath |
| 285 | + filepaths = args.merge |
| 286 | + filepaths.append(filename) |
| 287 | + PdfMerge(filepaths, args.password) |
| 288 | + |
| 289 | + # Convert the images into PDF, merge them with the PDF file(s) and encrypt the file |
| 290 | + elif args.image and args.password and (args.merge or args.filepath): |
| 291 | + if args.filepath: |
| 292 | + files = [args.filepath, "name.pdf"] |
| 293 | + PdfImage(args.image, True) |
| 294 | + PdfMerge(files, args.password) |
| 295 | + if os.path.isfile("name.pdf"): |
| 296 | + os.remove("name.pdf") |
| 297 | + else: |
| 298 | + PdfImage(args.image, True) |
| 299 | + filepaths = args.merge |
| 300 | + filepaths.append("name.pdf") |
| 301 | + PdfMerge(filepaths, args.password) |
| 302 | + if os.path.isfile("name.pdf"): |
| 303 | + os.remove("name.pdf") |
| 304 | + |
| 305 | + # Merge the single file and other files |
| 306 | + elif args.filepath and args.merge: |
| 307 | + filename = args.filepath |
| 308 | + filepaths = args.merge |
| 309 | + filepaths.append(filename) |
| 310 | + PdfMerge(filepaths) |
| 311 | + |
| 312 | + # Encrypt the PDF file |
| 313 | + elif args.filepath and args.password: |
| 314 | + PdfPassword(args.filepath, args.password) |
| 315 | + |
| 316 | + # Merge and encrypt the PDF files into one |
| 317 | + elif args.merge and args.password: |
| 318 | + PdfMerge(args.merge, args.password) |
| 319 | + |
| 320 | + # Encrypt Multiple files |
| 321 | + elif args.filepaths and args.password: |
| 322 | + PdfMultiplePassword(args.filepaths, args.password) |
| 323 | + |
| 324 | + # Merge Image and PDF files provided |
| 325 | + elif args.image and args.merge: |
| 326 | + PdfImage(args.image, True) |
| 327 | + filepaths = args.merge |
| 328 | + filepaths.append("name.pdf") |
| 329 | + PdfMerge(filepaths) |
| 330 | + if os.path.isfile("name.pdf"): |
| 331 | + os.remove("name.pdf") |
| 332 | + |
| 333 | + # Convert Images to individual PDF and encrypt them |
| 334 | + elif args.seprateimage and args.password: |
| 335 | + PdfMultipleImage(args.seprateimage, args.password) |
| 336 | + |
| 337 | + # Merge PDF files is provided |
| 338 | + elif args.merge: |
| 339 | + PdfMerge(args.merge) |
| 340 | + |
| 341 | + # Convert Images to single PDF |
| 342 | + elif args.image: |
| 343 | + PdfImage(args.image) |
| 344 | + |
| 345 | + # Convert Images to individual PDF |
| 346 | + elif args.seprateimage: |
| 347 | + PdfMultipleImage(args.seprateimage) |
| 348 | + |
| 349 | + |
| 350 | +if __name__ == '__main__': |
| 351 | + main() |
0 commit comments