Enable Dark Mode!
how-to-open-a-txt-file-and-read-its-data-in-odoo.jpg
By: Sree

How to Open A Text File and Read Its Data In Odoo?

Functional Odoo 14

A txt file is one of the forms of files that are available in a system such as docs, jpegs, pdfs, and many more. The simplest of the available files in the Text file may or may not be having a”.txt” extension. Moreover, a txt file only has a fewer bytes size with low content of data.

This blog will describe on How to open a txt file and read its data in Odoo.

Let’s now move on and understand about opening a txt file and reading its data.

To open a txt report in odoo we want to provide the place of the document. For that, we are importing os library.

Import os

This module provides a practical way to use the functionality of the operating system.

It provides many useful OS functions that are used to perform OS-based tasks and obtain related information about the operating system.

The  OS  comes under python’s standard utility modules. This module offers a portable way of using operating system dependent functionality.

Next, we can save the absolute path of the txt file to a variable

Path = os.path.expanduser(‘absolute path to .txt file’)

os.path.exoanduser() method in python is used to extend the first element of the path.

On Unix platforms, the initial ~ is replaced by HOME default value, if set.

Otherwise, the os.path.expanduser() method finds the user’s home directory in the password directory using the built-in pwd module. The path containing the initial user component is looked up directly in the password directory.

Next, we can open the file

txt_file_object = open(path,’r’)

The open() function opens a file and returns it as a file object.

Syntax : - open(file,mode)

Parameters

File:- The path and name of the file.

Mode: - specify which mode to open the file in:

“ r ” - Read mode - Opens the file for reading, it will show an error if the file does not exist

“ a ” - Append mode - Opens a file for appending, it will create the new if it does not exist.

“ w “ - write mode - open a file for writing, it will create the file if it does not exist.

“ x ” - Create mode - Create the specified file, it will return an error as binary or text mode

“ t “ - Text - Default value.

“ b ” - Binary -Binary mode.

Next, we can check how to implement this in Odoo.

For that, I am going to create a wizard, if you don’t know how to create a wizard in odoo you can refer to the following blog:https://www.cybrosys.com/blog/how-to-create-wizard-in-odoo-13


Inside the wizard, I have created a char field and an Html field.

file_path = fields.Char(string=” File path”)
note = fields.Html(‘Notes’)

Further, then I added these variables to the wizard XML field.

<field name=”file_path”/>
<field name=”note” widget=”html”/>

And Create a function to read data from a txt file and write it to the HTML field.

def action_open(self):
If self.file_path :
try:
   path = os.path.expanduser(self.file_path)
   txt_file_object - open(path,’r’)
    self.note = txt_file_object.read()
except:
    self.note = “Enter a Valid file”

Once the operation is validated you will obtain the following output.


how-to-open-a-txt-file-and-read-its-data-in-odoo

This way we can open a txt file in Odoo and read its contents. I hope that now you have an understanding of How to open a txt file and read its data in Odoo


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



0
Comments



Leave a comment

 
whatsapp
location

Calicut

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

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

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