Skip to content

Instantly share code, notes, and snippets.

@Muzzy73
Last active Jun 23, 2021
Embed
What would you like to do?
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

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