MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / embedded_copy

Function embedded_copy

src/__main__.py:288–325  ·  view source on GitHub ↗

Copy embedded files between PDFs.

(args)

Source from the content-addressed store, hash-verified

286
287
288def 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
328def embedded_del(args):

Callers

nothing calls this directly

Calls 9

embfile_namesMethod · 0.80
embfile_infoMethod · 0.80
embfile_getMethod · 0.80
embfile_addMethod · 0.80
saveIncrMethod · 0.80
open_fileFunction · 0.70
closeMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…