-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_readExcel.py
96 lines (77 loc) · 2.71 KB
/
_readExcel.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
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
import openpyxl
import os
from string import punctuation
template = """
# Link: %s
for loop in range(int(input())):
n,m = map(int,input().split())
l = list(map(int,input().split()))
"""
def convertCase(
arr
): # https://www.geeksforgeeks.org/convert-given-array-of-strings-to-a-camel-case-format-sentence/
ans = ""
try:
N = len(arr)
for i in range(N):
if (len(ans) > 0):
ans += ' '
ans += arr[i][0].upper()
j = 1
while j < len(arr[i]):
if (arr[i][j] == ' '):
ans += ' '
ans += arr[i][j + 1].upper()
j += 1
else:
ans += arr[i][j].lower()
j += 1
except:
pass
return ans.translate(str.maketrans('', '', punctuation))
def getNthLink(n: int):
nextFile = n-1
i = 6
val = 0
while (val != nextFile):
i += 1
if (ws.cell(row=i, column=2).value != None):
val += 1
requiredCell = ws.cell(row=i, column=2)
nextName = str(nextFile + 1) + '_' + convertCase(requiredCell.value.split()) + ".py"
cellLink = requiredCell.hyperlink.target
if (input("Do you want to overwrite the file? (y/n)") == 'y'):
with open(os.getcwd() + '/FINAL450/' + nextName.replace(" ", ""), 'w') as f:
f.write(template % (str(cellLink)))
print("catergory: " + ws.cell(row=i, column=1).value)
return cellLink
def getNextLink():
nextFile = 0
nextName = ''
for file in os.listdir(os.getcwd() + '/FINAL450/'):
try:
nextFile = max(nextFile, int(file.split('_')[0]))
except:
pass
print("getting number " + str(nextFile + 1) + " for you!!")
i = 6
val = 0
while (val != nextFile):
i += 1
if (ws.cell(row=i, column=2).value != None):
val += 1
requiredCell = ws.cell(row=i, column=2)
nextName = str(nextFile + 1) + '_' + convertCase(requiredCell.value.split()) + ".py"
cellLink = requiredCell.hyperlink.target
if (input("Do you want to overwrite the file? (y/n)") == 'y'):
with open(os.getcwd() + '/FINAL450/' + nextName.replace(" ", ""), 'w') as f:
f.write(template % (str(cellLink)))
print("catergory: " + ws.cell(row=i, column=1).value)
return cellLink
wb = openpyxl.load_workbook(os.getcwd() + '/FINAL450/0_FINAL450.xlsx')
ws = wb['Sheet1']
if (input("Do you want the next link? (y/n)") == 'y'):
print(getNextLink())
elif (input("Do you want a specific link? (y/n)") == 'y'):
print(getNthLink(int(input("Enter the number of the question: "))))
# This will fail if there is no hyperlink to target