midrange.com code scratchpad
Name:
python on IBM i
Scriptlanguage:
Python
Tabwidth:
4
Date:
11/13/2015 10:17:39 pm
IP:
Logged
Description:
my first try at python3 to xlsx
need to install xlsxwriter and supply userid and password

I think all the formatting holds....
Code:
  1. import ibm_db                                       
  2. import xlsxwriter                                   
  3.                                                     
  4. db_name  = '*LOCAL'                                 
  5. username = 'USERID'                                
  6. password = 'PASSWORD'                               
  7.                                                     
  8. wb = xlsxwriter.Workbook('/tmp/example-qcustcdt.xlsx')     
  9. ws = wb.add_worksheet('QCustCDT')                   
  10.                                                     
  11. conn = ibm_db.connect(db_name, username, password)  
  12. sql = 'SELECT * FROM QIWS.QCUSTCDT'                 
  13. if conn:                                            
  14.   stmt = ibm_db.exec_immediate(conn, sql)           
  15.   num_columns = ibm_db.num_fields(stmt)             
  16.   for num in range(0, num_columns):                      
  17.     ws.write_string(0, num, ibm_db.field_name(stmt, num))
  18.   x=1                                                    
  19.   while ibm_db.fetch_row(stmt):                          
  20.     for num in range(0, num_columns):                    
  21.       bd  = ibm_db.result(stmt, num)                     
  22.       ws.write_string(x, num, str(bd))                   
  23.     x+=1                                                 
  24. else:                                                    
  25.  print('Connection Failed')                              
  26.                                                          
  27. ibm_db.close(conn)                                       
  28. wb.close()                                               
  29.  
© 2004-2019 by midrange.com generated in 0.013s valid xhtml & css