Skip to Content
Menu
This question has been flagged
1 Reply
8296 Views

Plz i need to know how can i generate xls or csv file filled with data for exemple an array. Thank you.

Avatar
Discard
Thank you friend,
the thing is, creating a file is not a problem but my probleme that i want to generate a file after click event in a button to show the save file window. 

2015-11-19 19:45 GMT+01:00 Drees Far <far.drees@gmail.com>:

A new answer for generate xls or csv file has been posted. Click here to access the post.

--
Drees Far
Sent by Odoo S.A. using Odoo about Forum Post generate xls or csv file



--

 

Abderrahmen KHALLEDI
Développeur Odoo/OpenERP
Villa n°5, rue Haroun El Rachid Mutuelle ville
Tél. : +216 55 521 232
Mail : abderrahmen@techcare.tn
Linkedin : tn.linkedin.com/in/abderrahmenkhalledi/
Site : http://www.techcare-group.tn

Best Answer

Hello my friend;

here is some useful links that can help you a lot:

http://stackoverflow.com/questions/2084069/create-a-csv-file-with-values-from-a-python-list

http://stackoverflow.com/questions/14037540/writing-a-python-list-of-lists-to-a-csv-file

http://gis.stackexchange.com/questions/72458/export-list-of-values-into-csv-or-txt-file

https://docs.python.org/2/library/csv.html

You can use the ir.attachment module. I fact, you will create a .csv file in your pc and then a button which will call a function that you define in you python.And here is an example:

def print_declaration(self, cr, uid, ids, id, context=None):

print ids

dec = self.pool.get('declaration.cnss')

obj = dec.search(cr, uid, [('id','in', ids)])

cns = dec.read(cr, uid, obj, ['id', 'year','trimester','company_id'], context=False)

year = cns[0]['year']

company = cns[0]['company_id'][0]

print company

trimester = cns[0]['trimester']

filename="/opt/file.txt"

lines = open(filename).readlines()  

open(filename, 'w').writelines(lines[-1:-1])

writepath = '/opt/file.txt'

mode = 'a' if os.path.exists(writepath) else 'w' 

var = 0

i = 0

j = 0

page =1

res_list=[]

payslip_line = self.pool.get('hr.payslip')

obj_ids = payslip_line.search(cr, uid, [('company_id','=', company)]) 

for res in payslip_line.read(cr, uid, obj_ids, ['id', 'employee_id','date_from'], context=False):

date = res['date_from']

var = var + 1

if var == 12:

var = 1

obj = {}

obj['matricule'] = res['employee_id'][0]

a = str(obj['matricule'])

obj['numero'] = var

matricule = res['employee_id'][0]

obj['name'] = res['employee_id'][1]

payslip_lines = self.pool.get('hr.employee')

obj_ids2 = payslip_lines.search(cr, uid, [('id', '=', matricule)])

for res in payslip_lines.read(cr, uid, obj_ids2, ['id', 'n_cnss', 'momo_category','address_id','name_employee','surname_emloyee','name_fother','marital','gender','nic_id'], context=False):

marital = res['marital']

gender = res['gender']

nom_employe = res['name_employee']

prenom_employe = res['surname_emloyee']

nom_pere = res['name_fother']

if nom_employe== False:

My_error_Msg = 'Please, enter the name of your employee'

raise osv.except_osv(_("Error!"), _(My_error_Msg))

return False

if prenom_employe== False:

My_error_Msg = 'Please, enter the surname of your employee'

raise osv.except_osv(_("Error!"), _(My_error_Msg))

return False

if nom_pere== False:

My_error_Msg = 'Please, enter the surname of your Employee Fother'

raise osv.except_osv(_("Error!"), _(My_error_Msg))

return False

nom = prenom_employe+''+nom_employe

if gender=='male':

nom = prenom_employe+' B '+nom_pere+ ' ' +nom_employe

if gender=='female' and marital=='single':

nom = prenom_employe+' BT '+nom_pere+ ' ' +nom_employe

if gender=='female' and marital=='married':

nom = prenom_employe+ ' ' +nom_employe

if gender=='female' and marital=='widower':

nom = prenom_employe+' BT '+nom_pere+ ' ' +nom_employe

if gender=='female' and marital=='divorced':

nom = prenom_employe+' BT '+nom_pere+ ' ' +nom_employe

company = self.pool.get('res.partner')

company_ids = company.search(cr, uid, [('id','=',res['address_id'][0])]) 

for test in company.read(cr, uid, company_ids, ['id', 'company_id'], context=False):

companys = self.pool.get('res.company')

companys_ids = companys.search(cr, uid, [('id','=',test['company_id'][0])])

for tests in companys.read(cr, uid, company_ids, ['id', 'cnss'], context=False):

j = j + 1

if j %12==0:

page = page +1

c = str(0)

d = str(page)

pages = c+c+d

var1 = map(int, str(tests['cnss']))

var2 = var1[-1]

var3 = var1[-2]

var4 = int(str(var3) + str(var2))

var5 = var1.remove(var2)

var6 = var1.remove(var3)

s = map(str, var1)

s = ''.join(s)

s = int(s)

obj['matricule_employeur'] = s

obj['cle_employeur'] = var4

test = str(var4)

test2 = str(s)

test2 = '00'+test2

if len (test2) <> 8:

My_error_Msg = 'Taille de la matricule CNSS non compatible'

raise osv.except_osv(_("Erreur!"), _(My_error_Msg))

return False

taux_cotisation = str(0000)

variable1 = map(int, str(res['n_cnss']))

variable2 = variable1[-1]

variable3 = variable1[-2]

variable4 = int(str(variable3) + str(variable2))

cle_assure = variable4

variable5 = variable1.remove(variable2)

variable6 = variable1.remove(variable3)

matricule_assure = map(str, variable1)

matricule_assure = ''.join(matricule_assure)

if len (matricule_assure) <> 8:

My_error_Msg = 'Taille du numero Assure social non compatible'

raise osv.except_osv(_("Erreur!"), _(My_error_Msg))

return False

matricules_assure = str(matricule_assure)

cles_assure = str(cle_assure)

print ('---------------------------en test--------------------------------')

marital = res['marital']

gender = res['gender']

nom_employe = res['name_employee']

prenom_employe = res['surname_emloyee']

nom_pere = res['name_fother']

if nom_employe== False:

My_error_Msg = 'Please, enter the name of your employee'

raise osv.except_osv(_("Error!"), _(My_error_Msg))

return False

if prenom_employe== False:

My_error_Msg = 'Please, enter the surname of your employee'

raise osv.except_osv(_("Error!"), _(My_error_Msg))

return False

if nom_pere== False:

My_error_Msg = 'Please, enter the surname of your Employee Fother'

raise osv.except_osv(_("Error!"), _(My_error_Msg))

return False

nom = prenom_employe+''+nom_employe

if gender=='male':

nom = prenom_employe+' B '+nom_pere+ ' ' +nom_employe

if gender=='female' and marital=='single':

nom = prenom_employe+' BT '+nom_pere+ ' ' +nom_employe

if gender=='female' and marital=='married':

nom = prenom_employe+ ' ' +nom_employe

if gender=='female' and marital=='widower':

nom = prenom_employe+' BT '+nom_pere+ ' ' +nom_employe

if gender=='female' and marital=='divorced':

nom = prenom_employe+' BT '+nom_pere+ ' ' +nom_employe

if len(nom) > 60:

My_error_Msg = 'Please, The size of your employee name is incorrect'

raise osv.except_osv(_("Error!"), _(My_error_Msg))

return False

num_cin = res['nic_id']

cin = str(num_cin)

if num_cin ==False:

My_error_Msg = 'Please, enter the CIN of your employee'

raise osv.except_osv(_("Error!"), _(My_error_Msg))

return False

if len(cin) > 8:

My_error_Msg = 'Please, the size of the CIN of your employee is incorrect'

raise osv.except_osv(_("Error!"), _(My_error_Msg))

return False

taille_nom = len(nom)

nbr_espace = 60 - taille_nom

for i in range (nbr_espace):

nom = nom+' '

if taille_nom < 60:

nom = nom+''

brut_line = self.pool.get('declaration.cnss.line')

brut_ids= brut_line.search(cr, uid, [])

somme_brut1 = 0

somme_brut2 = 0

somme_brut3 = 0

for res in brut_line.read(cr, uid, brut_ids, ['id', 'brut','brut2','brut3'], context=False):

obj['brut'] = res['brut']

obj['brut2'] = res['brut2']

obj['brut3'] = res['brut3']

somme_brut1 = somme_brut1 + res['brut']

somme_brut2 = somme_brut2 + res['brut2']

somme_brut3 = somme_brut3 + res['brut3']

somme = res['brut'] + res['brut2'] + res['brut3']

somme_brut = somme_brut1 + somme_brut2 + somme_brut3

sommes = 1000*somme

obj['somme_brut1'] = somme_brut1

obj['somme_brut2'] = somme_brut2

obj['somme_brut3'] = somme_brut3

obj['somme_brut'] = somme_brut

obj['somme'] = sommes

salaire = str(int(sommes))

nbr_espace2 = 10 - len (salaire)

j = 0

for j in range (nbr_espace2):

salaire = '0'+salaire

a = str(var)

b = str(0)

ligne = b+a

print len (test2+test+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+trimester+year+pages+ligne+matricules_assure+cles_assure+nom+cin+salaire+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation)

with open(writepath, mode) as f:

f.write(test2+test+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+trimester+year+pages+ligne+matricules_assure+cles_assure+nom+cin+salaire+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+taux_cotisation+'\n')

res_list.append(obj)

filename="/opt/file.txt"

files = open(filename,'rb').read()

point = "."

nom_fichier = "DS"+test2+test+taux_cotisation+taux_cotisation+taux_cotisation+str(6)+point+trimester+year

files = open(filename,'rb').read().encode('base64')

ir_values={

'name': nom_fichier,

'datas_fname': nom_fichier,

'type': 'binary',

'datas': files,

'create_uid': uid,

# 'partner_id': ids[0],

'res_model': 'declaration.cnss',

'res_id': ids[0],

self.pool.get('ir.attachment').create(cr,uid,ir_values,context=context)

here is an example that i have wirked with but a text file and i am certain that it will work with the csv

Best Regards.

Avatar
Discard
Author

Thank you my friend, the thing is, creating a file is not a problem but my probleme that i want to generate a file after click event in a button to show the save file window.

its an example of a file that i have created and it works perfectly :) plz vote and upvote ;)

Related Posts Replies Views Activity
0
Sep 18
3037
0
Jan 17
4141
2
Feb 16
6650
0
Apr 24
399
4
Nov 23
4246