images-missing-relink.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. # © 2015.09.22 ale rimoldi <ale@graphicslab.org>
  3. # This program is free software under the MIT license.
  4. """
  5. USAGE
  6. You must have a document open.
  7. This script checks if the document contains image frames with a broken link.
  8. If any is found, it will ask if you want to look for the correct image and then
  9. prompt for the new location of the first image.
  10. It will then check all other image frames with a broken link, and look for an image
  11. of the same name in the new location. It the image is there, the link will be
  12. replaced.
  13. You would be wise to work on a copy of the original to avoid accidentally include
  14. the wrong images.
  15. """
  16. import scribus
  17. import os.path
  18. page_n = scribus.pageCount()
  19. item_missing = []
  20. # create a list of items with a brokein link
  21. for page_i in range(0, page_n) :
  22. scribus.gotoPage(page_i + 1)
  23. item_list = scribus.getPageItems()
  24. # print(item_list)
  25. for item in item_list :
  26. if item[1] == 2 :
  27. image_filepath = ""
  28. image_filepath = scribus.getImageFile(item[0])
  29. print(image_filepath)
  30. if image_filepath != "" and not os.path.isfile(image_filepath) :
  31. item_missing.append((item[0], image_filepath))
  32. print(item_missing)
  33. # read the link for the first image with a broken link and try to apply the path
  34. # to each other image with a broken link
  35. if item_missing :
  36. if scribus.messageBox("Missing images", "There are missing images. Do you want to look for them?", scribus.ICON_WARNING, scribus.BUTTON_YES, scribus.BUTTON_NO) == scribus.BUTTON_YES:
  37. filename_found = scribus.fileDialog("Find "+os.path.basename(item_missing[0][1]), "Image files (*."+os.path.splitext(item_missing[0][1])[1][1:]+")")
  38. # print(filename_found)
  39. if filename_found:
  40. path_found = os.path.dirname(filename_found)
  41. print("path_found "+path_found)
  42. for item in item_missing:
  43. item_filename = os.path.join(path_found, os.path.basename(item[1]))
  44. # print(item_filename)
  45. if os.path.isfile(item_filename) :
  46. scribus.loadImage(item_filename, item[0])