Skip to content

Instantly share code, notes, and snippets.

@Muzzy73
Last active February 19, 2023 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Muzzy73/c7262cff187a09672c8e8d2983f53d23 to your computer and use it in GitHub Desktop.
Save Muzzy73/c7262cff187a09672c8e8d2983f53d23 to your computer and use it in GitHub Desktop.
Script to upload images to ERPNext Item Page
import frappe
def execute():
files = frappe.get_all(
"File",
filters={"is_folder": 0},
fields=["name", "file_name", "file_url"],
)
for file in files:
fname, fext = file.get("file_name").split(".")
if fext not in ["png", "jpg", "jpeg"]:
continue
has_existing_item = _check_existing_item(fname)
if not has_existing_item:
continue
frappe.db.set_value("File", file.get("name"), "attached_to_doctype", "Item")
frappe.db.set_value("File", file.get("name"), "attached_to_name", fname)
item_doc = frappe.get_doc("Item", fname)
item_doc.image = file.get("file_url")
item_doc.save()
print("Item {} has been set.".format(fname))
def _check_existing_item(name):
return len(frappe.get_all("Item", filters={"name": name})) == 1
@Muzzy73
Copy link
Author

Muzzy73 commented Jun 22, 2021

Image file name should be exactly same as Item Name.

bench --site [site name] execute [dir name].image_script.execute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment