-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyOpenpyxl.py
More file actions
454 lines (322 loc) · 12.6 KB
/
PyOpenpyxl.py
File metadata and controls
454 lines (322 loc) · 12.6 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#-------------------------------------------------------------------------------
# Name: PyOpenpyxl.py
# Purpose:
#
# Author: Shyed Shahriar Housaini
#
# Created: 24/10/2019
# Copyright: (c) Shyed Shahriar Housaini 2019
# Licence: <your licence: Shyed Shahriar Housaini's terms and conditions.>
#-------------------------------------------------------------------------------
print(""" --------- Start of written code ----------
import openpyxl
import os
======= End of written code ========== End of written code ======= Output ======Output ====== """)
import openpyxl
import os
print(""" --------- Start of written code ----------
## os.chdir('C:\\Users\\HP\\Desktop') ## For office computer. ##
os.chdir('C:\\Users\\HP\\Desktop') ## For home computer. ##
## Always use \\ for avoiding any error in windows.
## use / or // for linux
======= End of written code ========== End of written code ======= Output ======Output ====== """)
## os.chdir('C:\\Users\\HP\\Desktop') ## For office computer. ##
os.chdir('C:\\Users\\HP\\Desktop') ## For home computer. ##
## Always use \\ for avoiding any error in windows.
## use / or // for linux
print(""" --------- Start of written code ----------
dirpath = os.getcwd()
print("current directory is : " + dirpath)
======= End of written code ========== End of written code ======= Output ======Output ====== """)
dirpath = os.getcwd()
print("current directory is : " + dirpath)
print(""" --------- Start of written code ----------
foldername = os.path.basename(dirpath)
print("Directory name is : " + foldername)
scriptpath = os.path.realpath(__file__)
print("Script path is : " + scriptpath)
print ("Current openpyxl version :" , openpyxl.__version__)
======= End of written code ========== End of written code ======= Output ======Output ====== """)
foldername = os.path.basename(dirpath)
print("Directory name is : " + foldername)
scriptpath = os.path.realpath(__file__)
print("Script path is : " + scriptpath)
print ("Current openpyxl version :" , openpyxl.__version__)
print(""" --------- Start of written code ----------
wb=openpyxl.load_workbook('C:\\Users\\HP\\Desktop\\Testopenpyxl.xlsx')
# type(wb) or print (wb) #
print (wb)
SN=wb.get_sheet_names()
print('sheet_names : ' , SN)
======= End of written code ========== End of written code ======= Output ======Output ====== """)
wb=openpyxl.load_workbook('C:\\Users\\HP\\Desktop\\Testopenpyxl.xlsx')
# type(wb) or print (wb) #
print (wb)
SN=wb.get_sheet_names()
print('sheet_names : ' , SN)
print(""" --------- Start of written code ----------
sheet= wb.get_sheet_by_name('Sheet1')
# type() or print () #
print (sheet)
======= End of written code ========== End of written code ======= Output ======Output ====== """)
sheet= wb.get_sheet_by_name('Sheet1')
# type() or print () #
print (sheet)
print(""" --------- Start of written code ----------
sheet['A1'].value
sheeta1=sheet['A1'].value
print(sheeta1)
sheet['A4'].value
sheeta4=sheet['A4'].value
print(sheeta4)
sheet['A7'].value
sheeta7=sheet['A7'].value
print(sheeta7)
======= End of written code ========== End of written code ======= Output ======Output ====== """)
sheet['A1'].value
sheeta1=sheet['A1'].value
print(sheeta1)
sheet['A4'].value
sheeta4=sheet['A4'].value
print(sheeta4)
sheet['A7'].value
sheeta7=sheet['A7'].value
print(sheeta7)
print(""" --------- Start of written code ----------
sheet['B1'].value
sheetB1=sheet['B1'].value
print(sheetB1)
sheet['B4'].value
sheetB4=sheet['B4'].value
print(sheetB4)
sheet['B7'].value
sheetB7=sheet['B7'].value
print(sheetB7)
======= End of written code ========== End of written code ======= Output ======Output ====== """)
sheet['B1'].value
sheetB1=sheet['B1'].value
print(sheetB1)
sheet['B4'].value
sheetB4=sheet['B4'].value
print(sheetB4)
sheet['B7'].value
sheetB7=sheet['B7'].value
print(sheetB7)
print(""" ~!@#$%^&* --------- Start of written code ---------- ~!@#$%^&*
sheet['C1'].value
sheetC1=sheet['C1'].value
print(sheetC1)
sheet['C4'].value
sheetC4=sheet['C4'].value
print(sheetC4)
sheet['C7'].value
sheetC7=sheet['C7'].value
print(sheetC7)
~!@#$%^&* ======= End of written code ========== End of written code ======= Output ======Output ====== ~!@#$%^&* """)
sheet['C1'].value
sheetC1=sheet['C1'].value
print(sheetC1)
sheet['C4'].value
sheetC4=sheet['C4'].value
print(sheetC4)
sheet['C7'].value
sheetC7=sheet['C7'].value
print(sheetC7)
print(""" --------- Start of written code ----------
sheet['C11'].value = 43
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
## can not save into currently open files.
======= End of written code ========== End of written code ======= Output ======Output ====== """)
sheet['C11'].value = 43
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
## can not save into currently open files.
print(""" --------- Start of written code ----------
STL=sheet.title
print(STL) ## current sheet name
======= End of written code ========== End of written code ======= Output ======Output ====== """)
STL=sheet.title
print(STL) ## current sheet name
print(""" --------- Start of written code ----------
sheet.title= "My wb sheet"
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
======= End of written code ========== End of written code ======= Output ======Output ====== """)
sheet.title= "My wb sheet"
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
print(""" --------- Start of written code ----------
STL2=sheet.title
print(STL2) ## current sheet name
======= End of written code ========== End of written code ======= Output ======Output ====== """)
STL2=sheet.title
print(STL2) ## current sheet name
print(""" --------- Start of written code ----------
row_column= sheet.cell(row=1, column=1).value
print(row_column)
======= End of written code ========== End of written code ======= Output ======Output ====== """)
row_column= sheet.cell(row=1, column=1).value
print(row_column)
print(""" --------- Start of written code ----------
row_column113= sheet.cell(row=11, column=3).value
## (row=11, column=3) = C11
print(row_column113)
======= End of written code ========== End of written code ======= Output ======Output ====== """)
row_column113= sheet.cell(row=11, column=3).value
## (row=11, column=3) = C11
print(row_column113)
print(""" --------- Start of written code ----------
for i in range (1,5):
print(sheet.cell (row=i, column=3).value)
======= End of written code ========== End of written code ======= Output ======Output ====== """)
for i in range (1,5):
print(sheet.cell (row=i, column=3).value)
print(""" --------- Start of written code ----------
## for knowing maximum numbers of rows and columns
##sheet.max_row
##sheet.max_column
======= End of written code ========== End of written code ======= Output ======Output ====== """)
## for knowing maximum numbers of rows and columns
##sheet.max_row
##sheet.max_column
print(""" --------- Start of written code ----------
print(sheet.max_row)
print(sheet.max_column)
print(openpyxl.utils.get_column_letter(1))
======= End of written code ========== End of written code ======= Output ======Output ====== """)
print(sheet.max_row)
print(sheet.max_column)
print(openpyxl.utils.get_column_letter(1))
print(openpyxl.utils.get_column_letter(1))
print(""" --------- Start of written code ----------
## print(openpyxl.cell.get_column_letter(1))
## does not work rather use
## print(openpyxl.utils.get_column_letter(1)) ##
======= End of written code ========== End of written code ======= Output ======Output ====== """)
## print(openpyxl.cell.get_column_letter(1))
## does not work rather use
## print(openpyxl.utils.get_column_letter(1)) ##
print(""" --------- Start of written code ----------
print(openpyxl.utils.get_column_letter(26))
print(openpyxl.utils.get_column_letter(27))
======= End of written code ========== End of written code ======= Output ======Output ====== """)
print(openpyxl.utils.get_column_letter(26))
print(openpyxl.utils.get_column_letter(27))
print(""" --------- Start of written code ----------
print(openpyxl.utils.column_index_from_string('AA'))
print(openpyxl.utils.column_index_from_string('AAA'))
======= End of written code ========== End of written code ======= Output ======Output ====== """)
print(openpyxl.utils.column_index_from_string('AA'))
print(openpyxl.utils.column_index_from_string('AAA'))
print("""
--------- Start of written code ---------- --------- Start of written code ----------
wb.create_sheet(title="My 2nd sheet", index=1)
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
======= End of written code ========== End of written code ======= Output ======""")
wb.create_sheet(title="My 2nd sheet", index=1)
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
print("""
--------- Start of written code ---------- --------- Start of written code ----------
sheet.row_dimensions[1].height =71
sheet.row_dimensions[11].height =77
sheet.column_dimensions['E'].width = 43
sheet.column_dimensions['D'].width = 34
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
======= End of written code ========== End of written code =======
output ======
""")
sheet.row_dimensions[1].height =71
sheet.row_dimensions[11].height =77
sheet.column_dimensions['E'].width = 43
sheet.column_dimensions['D'].width = 34
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
print("""
--------- Start of written code ---------- --------- Start of written code ----------
print(sheet.row_dimensions[1].height)
print(sheet.row_dimensions[2].height)
print(sheet.column_dimensions['A'].width)
print(sheet.column_dimensions['B'].width)
======= End of written code ========== End of written code =======
output ======
""")
print(sheet.row_dimensions[1].height)
print(sheet.row_dimensions[2].height)
print(sheet.column_dimensions['A'].width)
print(sheet.column_dimensions['B'].width)
print("""
--------- Start of written code ---------- --------- Start of written code ----------
reload the workbook
wb=openpyxl.load_workbook('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
sheet=wb.get_sheet_by_name('My wb Sheet')
type(sheet)
print(sheet)
======= End of written code ========== End of written code =======
output ======
""")
##reload the workbook
wb=openpyxl.load_workbook('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
sheet=wb.get_sheet_by_name('My wb sheet')
## type(sheet) ## this does not work
print(sheet)
print("""
--------- Start of written code ---------- --------- Start of written code ----------
from openpyxl.styles import Font
sheet['B1'].font=Font(sz=14, bold=True, italic=True)
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
======= End of written code ========== End of written code =======
output ======
""")
from openpyxl.styles import Font
sheet['B1'].font=Font(sz=14, bold=True, italic=True)
sheet['B1'].font=Font(sz=14, bold=True, italic=True)
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
print("""
--------- Start of written code ---------- --------- Start of written code ----------
import random
## Create sheet by
wb.create_sheet(title="My 3rd sheet", index=2)
## OR Create sheet by
sheet=wb.create_sheet('My 4th sheet')
for i in range (1, 111, 2):
sheet['A' + str (i)].value = random.randint(1,1111)
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
======= End of written code ========== End of written code =======
output ======
""")
import random
## Create sheet by
wb.create_sheet(title="My 3rd sheet", index=2)
## OR Create sheet by
sheet=wb.create_sheet('My 4th sheet')
for i in range (1, 111, 2):
sheet['A' + str (i)].value = random.randint(1,1111)
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
for i in range (1, 111):
sheet['B' + str (i)].value = random.randint(1,9999)
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
print("""
--------- Start of written code ---------- --------- Start of written code ----------
refObj= openpyxl.chart.Reference(sheet(1,1),(10,1))
wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
print(refObj)
======= End of written code ========== End of written code =======
output ======
""")
import os
from selenium import webdriver
borwser = webdriver.Chrome()
browser.get('https://www.youtube.com')
elem = browser.find_element_by_css_selector('#description > yt-formatted-string > a:nth-child(1)')
elem.texy
elem.click()
browser.quit()
##refObj= openpyxl.chart.Reference(sheet(1,1),(10,1))
##wb.save('C:\\Users\\HP\\Desktop\\2ndTestopenpyxl.xlsx')
##print(refObj)
print("""
--------- Start of written code ---------- --------- Start of written code ----------
======= End of written code ========== End of written code =======
output ======
""")
print("""
--------- Start of written code ---------- --------- Start of written code ----------
======= End of written code ========== End of written code =======
output ======
""")