How to handle CSV files in odoo17

TEAM-BASSAM
June 28, 2024
csv-files-in-odoo17

We need to generate several CSV files in Odoo 17 for different purposes or XML files are commonly used for data collection and various systems for tasks like manual integration. CSV files in odoo17 are structured in a specific way. and separated data by commas, colons, and semi-colons, We can now import the ‘CSV’ package and conduct CSV file operations. let’s explore how to handle CSV files in Odoo 17.

Import CSV Files in Odoo 17

After importing CSV files in odoo17, you can add new entries, change current records, and execute bulk actions effectively. Odoo offers a simple import interface for mapping CSV columns to Odoo model fields and managing different import circumstances. To import.

Go to the appropriate model’s list view.

Click the “Import” button and select a CSV file to upload. Use the import wizard to map CSV columns to their respective model fields, specify import parameters, and validate data. After confirmation, run the import process, and Odoo will generate or update entries based on the CSV data.

In this example, we will save record data as a CSV file. Consider the instance in which we export product records. To export, we must first build a CSV file. The filename is csv. The mode for writing a file must be ‘w’. Next, we need to utilize the writer function to write data into the file.

output = io.StringIO()
 writer = csv.writer(output, delimiter=',', quotechar='"',quoting=csv.QUOTE_MINIMAL)
 writer.writerow(['Product Name', 'UOM', 'Price'])
 for product in self.env['product.product'].search([]):
   name = product.product_tmpl_id.name
   uom = product.product_tmpl_id.uom_id.name
   price = product.product_tmpl_id.list_price
   writer.writerow([name, uom, price])
   data = output.getvalue().encode('utf-8')
 return data

delimiter: which defines the separator.

quote char: which defines the data enclosed with.

quoting: it has some values,

csv.QUOTE_ALL – Quote everything, regardless of type.

csv.QUOTE_MINIMAL – Quote fields with special characters.

csv.QUOTE_NONNUMERIC – Quote all fields that aren’t numbers value.csv.QUOTE_NONE – Don’t quote anything in the output

def export_customers(self):
import importlib
 importlib.reload(base64)
 data = self.env['export.csv'].export_product()
 self.csv_data = base64.b64encode(data).decode('utf-8')
 self.filename = 'export.csv'
 return {
   'type': 'ir.actions.act_url',
   'url': "web/content/?model=wiazrd.export.csv&id=" + str(self.id) +
    "&filename=export.csv&field=csv_data&download=true&filename=" + self.filename,
   'target': 'self',

The code imports modules dynamically using import lib, usually reloading base64. It fetches product details, decodes data into a string, and sets the desired filename for downloading CSV files in Odoo 17. This helps manage data exchange in Odoo 17.

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

Your email address will not be published. Required fields are marked *