Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #########################
- # Excel
- #########################
- #allows copying data from one sheet to an other one
- #find and elaborate some rows in a sheet
- #xlsx file is a workbook
- import openpyxl
- import os
- # read excel sheet
- os.chdir(sDirectory)
- workbook = openpyxl.load_workbook(sFileName)
- type(workbook)
- sheet = workbook.get_sheet_by_name(sSheetName)
- sheetList = workbook.get_sheet_names()
- cell = sheet['A1'] #get the cell object A1
- cell.value #depending on how the excel is formatted it can return a type
- str(cell.value)
- sheet.cell(row=1, colmn=2) #alternative to the cell coordinations
- for i in range(1, 8) :
- print(i, sheet.cell(row=i, column=2).value)
- # write excel sheet
- wb = openpyxl.Workbook() #create a new excel sheet
- wb.get_sheet_names() #contains the default Sheet
- wb.get_sheet_by_name('Sheet')
- sheet['A1'].value == None //True
- sheet['A1'] = 42
- sheet['A2'] = 'Hello'
- wb.save('example.xlsx') #create on disk the new excel sheet
- sheet2 = wb.create_sheet() #default to Sheet1
- sheet2.title = 'new name'
- wb.save(sFileName)
- wb.create_sheet(index=0, title='my other sheet') #createa at index 0 with proper title
Add Comment
Please, Sign In to add comment