/image_script.py Secret
Last active
February 19, 2023 14:34
Star
You must be signed in to star a gist
Script to upload images to ERPNext Item Page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Image file name should be exactly same as Item Name.
bench --site [site name] execute [dir name].image_script.execute