Skip to content

Instantly share code, notes, and snippets.

Last active Jun 23, 2021
What would you like to do?
Script to upload images to ERPNext Item Page
import frappe
def execute():
files = frappe.get_all(
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"]:
has_existing_item = _check_existing_item(fname)
if not has_existing_item:
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")
print("Item {} has been set.".format(fname))
def _check_existing_item(name):
return len(frappe.get_all("Item", filters={"name": name})) == 1

This comment has been minimized.

Copy link
Owner Author

@Muzzy73 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