import openpyxl ---> import lib
import os ----> import lib
cwd=os.getcwd()
os.chdir('path')
os.list('.')
wb = openpyxl.load_workbook('file_path')
type(wb) --- > type of excel file
wb.get_sheet_names() --> get sheet names of that excel file
sheet1=wb.get_sheet_by_name( ' sheet_name ' )
type(sheet1)
sheet1.title
sheet1.max_row
sheet1.max_column
top_sheet= wb.active --- > sheet that's on top when the workbook is open in Excel
wb.create_sheet(title='sheet_name')
wb.create_sheet(index=1, title= 'At index 1' ) --> create a sheet is at index 1 on workbook of Excel
wb.save(new_excel) --> save a new excel file
wb.remove_sheet('sheet_name')
Read and Write data at cell level
sheet1['Cell'].value
a=sheet1['Cell']
a.row
a.column
c=sheet1.cell(row=2,column=3)
c.value
lista=[]
for i in range(1,sheet.max_column+1):
e=sheet,cell(row=1,column=i)
print (e.value,end=' ')
lista.append(e.value)
for item in lista:
print (item)
for i in range(0,len(lista)):
e=sheet1.cell(row=2,column=i+3)
e.value=lista[i]
for j in range(1,sheet.max_row+1):
e=sheet1.cell(row=j,column=1)
print (e.value)
listab=[]
for i in range(1,sheet.max_row+1)
for j in range(1,sheet.max_column+1)
e=sheet.cell(row=i,column=j)
print (e.value)
listab[r-1].append(e.value)
e.value=listab[i-1][j-1]
Open a new text file and write the contents of countyData to it.
for rowNum in range(2, sheet.max_row): # skip the first row
❷ produceName = sheet.cell(row=rowNum, column=1).value
❸ if produceName in PRICE_UPDATES:
sheet.cell(row=rowNum, column=2).value = PRICE_UPDATES[produceName]
❹ wb.save('updatedProduceSales.xlsx')
Setting the Font Style of Cells
from openpyxl.styles import Font
import openpyxl ---> import lib import os ----> import lib cwd=os.getcwd() os.chdir('path') os.list('.') wb = openpyxl.load_workbook('file_path') type(wb) --- > type of excel file wb.get_sheet_names() --> get sheet names of that excel file sheet1=wb.get_sheet_by_name( ' sheet_name ' ) type(sheet1) sheet1.title sheet1.max_row sheet1.max_column top_sheet= wb.active --- > sheet that's on top when the workbook is open in Excel wb.create_sheet(title='sheet_name') wb.create_sheet(index=1, title= 'At index 1' ) --> create a sheet is at index 1 on workbook of Excel wb.save(new_excel) --> save a new excel file wb.remove_sheet('sheet_name')
Read and Write data at cell level
sheet1['Cell'].value a=sheet1['Cell'] a.row a.column c=sheet1.cell(row=2,column=3) c.value lista=[] for i in range(1,sheet.max_column+1): e=sheet,cell(row=1,column=i) print (e.value,end=' ') lista.append(e.value)
for item in lista: print (item)
for i in range(0,len(lista)): e=sheet1.cell(row=2,column=i+3) e.value=lista[i]
for j in range(1,sheet.max_row+1): e=sheet1.cell(row=j,column=1) print (e.value)
listab=[] for i in range(1,sheet.max_row+1) for j in range(1,sheet.max_column+1) e=sheet.cell(row=i,column=j) print (e.value) listab[r-1].append(e.value) e.value=listab[i-1][j-1]
Open a new text file and write the contents of countyData to it.
print('Writing results...') resultFile = open('census2010.py', 'w') resultFile.write('allData = ' + pprint.pformat(countyData)) resultFile.close() print('Done.')
Loop through the rows and update the prices.
for rowNum in range(2, sheet.max_row): # skip the first row ❷ produceName = sheet.cell(row=rowNum, column=1).value ❸ if produceName in PRICE_UPDATES: sheet.cell(row=rowNum, column=2).value = PRICE_UPDATES[produceName]
❹ wb.save('updatedProduceSales.xlsx')
Setting the Font Style of Cells from openpyxl.styles import Font
Font Objects
Formulas
Setting Row Height and Column Width
Merging and Unmerging Cells
Freeze Panes
Charts
github.com/yanGT