import openpyxl
import os
os.getcwd()
os.chdir(path)
wb=openpyxl.load_workbook('...xlsx')
type(wb)
wb.get_sheet_names()
sheet=wb.get_sheet_by_name('pestanya')
type(sheet)
sheet.title
sheet.max_row
sheet.max_column
sheet.title='nou nom'
wb.save('file.xlsx')
wb.create_sheet(index=<posición>,title='titol')
std=wd.get_sheet_by_name('titol')
wb.remove_sheet(std)
wb.save('file.xlsx')
sheet=wb.get_sheet_by_name('pestanya')
sheet['B2'].value
sheet['B2']='nou valor'
e=sheet['B2']
e.value
e.row
e.column
e.value='nou valor'
e=sheet.cell(row=2,column=2)
wb.save('file.xlsx')
3.Reading and Writing Rows in Excel
for i in range(1,sheet.max_column+1):
e=sheet.cell(row=1,column=i)
print(e.value,end=' ')
lista = []
for i in range(1,5):
e=sheet.cell(row=5,column=i)
lista.append(e.value)
for i in range(0,len(lista)):
e=sheet.cell(row=10,column=7+i)
e.value=list[i]
wb.save('file.xlsx')
4.Reading and Writing Column and later Grid in a Sheet
for i in range(1,sheet.max_row+1):
e=sheet.cell(row=j,column=2)
print(e.value)
lista = []
for i in range(1,5):
e=sheet.cell(row=i,column=2)
lista.append(e.value)
wb2=openpyxl.load_workbook('file2.xlsx')
sheet2=wb2.get_sheet_by_name('Sheet3')
for i in range(0,len(lista)):
e=sheet2.cell(row=10+i,column=2)
e.value=listb[i]
wb2.save('file2.xlsx')
### define 2D list
rows=6
columns=4
lista=[]
for i in range(1,rows+1):
lista.append([]) # add list into a list
# read data
for r in range(1,rows+1):
for c in range(1,columns+1):
e=sheet.cell(row=r,column=c)
lista[r-1].append(e.value)
# write data
for r in range(1,rows+1):
for c in range(1,columns+1):
j=sheet2.cell(row=r,column=c)
j.value=lista[r-1][c-1]
# save