edit xlsx file python code example

Example 1: how to write xlsx file in python

import openpyxl

## initializing the xlsx
xlsx = openpyxl.Workbook()

## creating an active sheet to enter data
sheet = xlsx.active

## creating data to append
data = [
         [1, 2, 3],
         [4, 5, 6],
         [7, 8, 9],
         [10, 11, 12]
       ]

## appending row by row to the sheet
for row in data:
    ## append method is used to append the data to a cell
    sheet.append(row)

## saving the xlsx file using 'save' method
xlsx.save('appending.xlsx')

Example 2: update xls file using python

"""
Enter the following command `sudo pip install openpyxl
if this doesn't work download openpyxl zip file from 
https://pypi.org/project/openpyxl/
after that extract it and then open the folder in terminal and write 
`sudo python setup.py install`

""""
import openpyxl
try:
    path = "pathOfxlsFile/filename.xls"
    wb_obj = openpyxl.load_workbook(path.strip())
    # from the active attribute
    sheet_obj = wb_obj.active
    # get max column count
    max_column = sheet_obj.max_column
    max_row = sheet_obj.max_row
    print(f"\n\n coulumns {max_column}  \n\n")
    print(f"\n\n rows{max_row}  \n\n")
    for j in range(1,max_row):
        #count starts form 1, don't use 0 
    	cellThatIsToBeChanged = sheet_obj.cell(row=j, column=1)
       	cellThatIsToBeChanged.value = 'new modified value'
        wb_obj.save('fileName.xlsx')
    except Exception as e:
        print(e)