Enable Dark Mode!
how-to-handle-csv-file-operations-odoo-13.png
By: Hajaj Roshan

How to Handle CSV File Operations in Odoo 13

Technical Odoo 13

If we take any leading ERP systems, we need different operations with data import or export. In Odoo we have similar features to data import and export. By default, Odoo uses XLS files to do such operations. But we can use XML files or CSV files instead of XLS because CSV and XML also do a similar job of XLS.
 
CSV files are the text files that are arranged data in a specific way. Data processing in CSV is easier. The data arranged in the CSV files are separated by a comma, semi-colon, colon and so on. So each row contains the number of columns. 

Let us see where we can use these CSV files in Odoo. Odoo users are most of the cases that deals with integration with other systems. If there is no option available for automatic integration we need to migrate data from one to another. In this kind of situation, we use the CSV files to do such operations. Because it’s easier to arrange CSV files into a table format. For example, consider the following table and how the table converted into a CSV file.


Programming Languages
File Extensions
Java
.java
C
.c
Python
.py

This table converts to a CSV file:
 
‘Programming Languages’,‘File Extensions;
‘Java’,’.java’
‘C’,’.c’
‘Python’,’.py’
 
In Odoo we can use a python package to handle the CSV file operations called ‘CSV’. This ‘CSV’ package is quite simple to understand the functionalities and operations. Let us see the read and write operations of the CSV file within the Odoo environment.
 
First Import package to the python file:
 
import csv
 
Let's see how to write data from a record. Here we taking an example of products.

The first step we create a new file. In our example, product.csv is the file name. For writing a file we use the mode ‘w’.

 with open('product.csv', mode='w') as file:
 
Next, we have to write data into the file. Using writer function we can write data to the file. For each row, we will use loop and will go through each record. To write each row call writerrow() function.
 
writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
# create a row contains heading of each column
writer.writerow(
     [‘Product Name’, ‘UOM’, ‘Price’, ‘Amount’])
# fetch products and write respective data.
for product in self.env[‘product.product’]:
  name = product.product_tmpl_id.name
  uom = product.product_tmpl_id.uom_id.name
  price = product.product_tmpl_id.list_price
  amount = product.product_tmpl_id.taxes_id.amount
  writer.writerow(
     [name, uom, price, amount,]
 
delimiter: This parameter’s value is used to separate the columns.
quotechar: This is used to set the value of the field enclosed with like ‘ or “
quoting: This parameter has the following values,
    csv.QUOTE_ALL - Quote everything, regardless of type.
    csv.QUOTE_MINIMAL - fields are quote with special characters.
    csv.QUOTE_NONNUMERIC - Only non-numerical values will quote.
    csv.QUOTE_NONE –  fields are not quoted whether it is numerical or not.
 
In this example first, we use the writer function to write the header part into the file. Then we looped through the records and taken the only necessary fields from the records. 
 
If you want to read the file using the following code:
 
with open('product.csv', 'r', encoding="utf-8") as f2:
   # file encode and store in a variable ‘data’
   data = str.encode(f2.read(), 'utf-8')
 
Next, we need to get the file. So we return this data into a wizard.
 
class ExportProductWizard(models.TransientModel):
   _name = 'wiazrd.product.csv'
   csv_product = fields.Binary()
   name_file = fields.Char()
   # function will trigger from the export button in customer export
   def export_customers(self):
       # call the function generate csv files
       file = self.env['product.csv'].export_product()  
       self.csv_product = base64.encodestring(file)
       self.name_file = ‘product.csv'
       return {
           'type': 'ir.actions.act_url',
           'url': "web/content/?model=wizard.product.csv&id=" + str(self.id) +
                  "&filename=product.csv&field=csv_product&download=true&name_file=" + self.name_file,
           'target': 'self',     
}


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message