Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

openpyxl xls - Create xlsx work sheet, write data to it, save xlsx file

from openpyxl import Workbook

workbook = Workbook()

# create xls workbook sheet
workbook.create_sheet(index=0, title='Sheet1')
workbook.create_sheet(index=0, title='Sheet2')

# remove xls workbook sheet
workbook.remove(workbook['Sheet2'])

# get active work sheet
work_sheet = workbook.active

data = [
    ['Naveen', 'M', 'Software Engineer'],
    ['David', 'N', 'Designer'],
    ['Shera', 'H', 'Web Developer']
]

rows = 3
cols = 3
# iterate to set data
for row_num in range(0, rows):
    for col_num in range (0, cols):
        work_sheet.cell(
            row=row_num + 1,
            column=col_num + 1
        ).value = data[row_num][col_num]

# save data to xls file
workbook.save('output.xlsx')
Source by learnbatta.com #
 
PREVIOUS NEXT
Tagged: #openpyxl #xls #Create #xlsx #work #write #data #save #xlsx #file
ADD COMMENT
Topic
Name
4+4 =