Working with excel in python

Working with excel in python

1.Writing to an excel sheet using Python 

import xlwt 
from xlwt import Workbook 
  
# Workbook is created 
wb = Workbook() 
  
# add_sheet is used to create sheet. 
sheet1 = wb.add_sheet('Sheet 1') 
  
sheet1.write(1, 0, 'arya') 
sheet1.write(2, 0, 'drj') 
sheet1.write(3, 0, 'ayesha') 
sheet1.write(4, 0, 'twinkle') 
sheet1.write(5, 0, 'aryadrj') 
sheet1.write(1, 2, 'delhi') 
sheet1.write(2, 2, 'nepal') 
sheet1.write(3, 2, 'america') 
sheet1.write(4, 2, 'usa') 
sheet1.write(5, 2, 'UA') 
  
wb.save('xlwt example.xls') 

Explanation:


  • Using xlwt module, one can perform multiple operations on spreadsheet. For example, writing or modifying the data can be done in Python.
  • sheet1.write(1, 0, 'arya') ,1 is column,0 is row.
  • arya is value/data of field.
  • wb.save('xlwt example.xls')  contain name of excel file.

Output:




Error part:

1.Error1

File "C:\Python37\lib\site-packages\xlwt\CompoundDoc.py", line 262, in save
    f = open(file_name_or_filelike_obj, 'w+b')
PermissionError: [Errno 13] Permission denied: 'xlwt example.xls'

Solution: close the excel file and run the program and open again that excel file.

2.Adding style to cel

import xlwt 
from xlwt import Workbook 
  
wb = Workbook() 
  
sheet1 = wb.add_sheet('Sheet 1') 
  
style  = xlwt.easyxf('font: bold 1,color red')
style1 = xlwt.easyxf('font: bold 1,color blue')
style2 = xlwt.easyxf('font: bold 1,color yellow')
style3 = xlwt.easyxf('font: bold 1,color green')
style4 = xlwt.easyxf('font: bold 1,color pink')
sheet1.write(1, 0, 'arya', style)
sheet1.write(2, 0, 'drj',style) 
sheet1.write(3, 0, 'ayesha',style1) 
sheet1.write(4, 0, 'twinkle',style2) 
sheet1.write(5, 0, 'aryadrj',style3) 
sheet1.write(1, 2, 'delhi',style4) 
sheet1.write(2, 2, 'nepal',style2) 
sheet1.write(3, 2, 'america',style3) 
sheet1.write(4, 2, 'usa',style1) 
sheet1.write(5, 2, 'UA',style2) 
  
wb.save('xlwt example.xls') 

Output:


Comments

Popular posts from this blog

Invalid syntax , perhaps you forgot a comma? Error in Python

MAD Project (Mobile Android Application Development)

Interview/Exam Questions on List in Python.