Copy embedded files between PDFs.
(args)
| 286 | |
| 287 | |
| 288 | def embedded_copy(args): |
| 289 | """Copy embedded files between PDFs.""" |
| 290 | doc = open_file(args.input, args.password, pdf=True) |
| 291 | if not doc.can_save_incrementally() and ( |
| 292 | not args.output or args.output == args.input |
| 293 | ): |
| 294 | sys.exit("cannot save PDF incrementally") |
| 295 | src = open_file(args.source, args.pwdsource) |
| 296 | names = set(args.name) if args.name else set() |
| 297 | src_names = set(src.embfile_names()) |
| 298 | if names: |
| 299 | if not names <= src_names: |
| 300 | sys.exit("not all names are contained in source") |
| 301 | else: |
| 302 | names = src_names |
| 303 | if not names: |
| 304 | sys.exit("nothing to copy") |
| 305 | intersect = names & set(doc.embfile_names()) # any equal name already in target? |
| 306 | if intersect: |
| 307 | sys.exit(f"following names already exist in receiving PDF: {str(intersect)}") |
| 308 | |
| 309 | for item in names: |
| 310 | info = src.embfile_info(item) |
| 311 | buff = src.embfile_get(item) |
| 312 | doc.embfile_add( |
| 313 | item, |
| 314 | buff, |
| 315 | filename=info["filename"], |
| 316 | ufilename=info["ufilename"], |
| 317 | desc=info["desc"], |
| 318 | ) |
| 319 | pymupdf.message(f"copied entry '{item}' from '{src.name}'") |
| 320 | src.close() |
| 321 | if args.output and args.output != args.input: |
| 322 | doc.save(args.output, garbage=3) |
| 323 | else: |
| 324 | doc.saveIncr() |
| 325 | doc.close() |
| 326 | |
| 327 | |
| 328 | def embedded_del(args): |
nothing calls this directly
no test coverage detected
searching dependent graphs…