-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworkingWithExcel.py
75 lines (47 loc) · 1.67 KB
/
workingWithExcel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import openpyxl
workbook = openpyxl.load_workbook('cwd_file.xlxs')
# a sinle xlxs file might contain multiple tabs and
# or maybe you can call that a sheet
# that little tab thingy in the left-bottom of your
# excel file??? yeah, that's what it is... that's a sheet
# you can select a sheet by
sheet1 = workbook.get_sheet_by_name('sheet_name_i.e_sheet1')
# get names of all sheets, no paras
sheets = workbook.get_sheet_names()
# print (sheets) will result in sheet1
# let's suppose we get a sheet named sheet1, let's work with it
cellOrColumnName = sheet1['firstColumnName']
# [] bracket in sheet gets call objects
# cell objects have value member variable
# with all the contents of that cell.
store = str(cellOrColumnName.value)
# cell() method retusn a Cell object from a sheet
# EDITING EXCEL SPREADSHEETS
wb = openpyxl.workbook()
# create a workbook object
wb.get_sheet_names()
# ['Sheet'] -> default name prolly.
# lets select this sheet and work with it
sheet = wb.get_sheet_by_name('Sheet')
sheet['A1'].value == None
# returns true
sheet['A1'] = 42
sheet['A2'] = 'Hello'
# save the sheet to some other directory.
import os
os.chdir('c:\\Users\\UserName\\Documents')
wb.save()
# create another sheet
sheet2 = wb.create_sheet()
wb.get_sheet_names()
# would returns 2 sheets now
# [Sheet1, Sheet2]
sheet2.title = 'New Sheet Of People Name'
wb.get_sheet_names()
# now it will return sheet1 and New Sheet of People Name
wb.save('example.xlsx')
# workbook saved
wb.create_sheet(index=0, title='My Other Sheet')
wb.save('Other Sheet.xlsx')
# Now, this workbook would have first sheet named my other sheet
# as you can see the first para index=0, represents first sheet index