Custom data import tool

custom data import file upload csv and data import in attence doctype any example`

frappe.ui.form.on(“Data Import Attendece”, {
refresh(frm) {
frm.add_custom_button(__(‘Import Attendance’), function() {
// Trigger file upload process
frappe.prompt({
label: __(‘Upload CSV File’),
fieldname: ‘csv_file’,
fieldtype: ‘Attach’,
reqd: 1
}, function(values){
// Once the user selects a file, upload it
frappe.call({
method: ‘learn.learn.api.import_attendance_from_csv’,
args: { file_data: values.csv_file },
callback: function(response) {
// Display response to the user
frappe.msgprint(response.message);
}
});
}, __(‘Submit’));
});
},
});

from future import unicode_literals
import frappe
from frappe.model.document import Document
import PyPDF2

@frappe.whitelist(allow_guest=True)
def import_attendance_from_csv(file_data):
try:
# Save the attached file
if file_data:
print(“file_data”, file_data)
file_doc = save_file(“Attendance Import.csv”, file_data, “File”, “File”)
file_path = frappe.get_site_path(“public”, file_doc.file_url[1:])
print(“file_path”, file_path)

        with open(file_path, 'r') as file:
            print("file", file)

            csv_reader = csv.DictReader(file)
            print(csv_reader)
            for row in csv_reader:
                print(row['Employee'])
                print(row['Full Name'])
                print("row", row)
                # Assuming column names are 'Employee', 'Full Name', 'Department', 'Company', and 'Status'
                employee_id = row.get('Employee')
                print(employee_id)
                full_name = row.get('Full Name')
                department = row.get('Department')
                company = row.get('Company')
                status = row.get('Status')

                # Create Attendance record
                attendance_doc = frappe.new_doc('Attendance')
                attendance_doc.employee = employee_id
                attendance_doc.full_name = full_name
                attendance_doc.department = department
                attendance_doc.company = company
                attendance_doc.status = status
                attendance_doc.insert(ignore_permissions=True)

            frappe.db.commit()

        return "Attendance imported successfully."
    else:
        return "No file data provided."
except Exception as e:
    frappe.log_error("Error importing attendance: {0}".format(str(e)))
    return "Error importing attendance: {0}".format(str(e))

`