-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_excel.py
More file actions
66 lines (49 loc) · 1.79 KB
/
scrape_excel.py
File metadata and controls
66 lines (49 loc) · 1.79 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
# Script to scrape a directory with excel files
# Import libraries
import os
import pandas as pd
def load_csv():
sun = "C:\Users\rcxsm\Documents\python_scripts\in\zonnecollectoren_rznstrt_5\zonnecollectoren.csv"
def do_your_thing(file):
print (file)
workbook = pd.read_excel(file)
h = workbook.head()
#print (workbook)
#print (h)
month_ = (workbook['Smit Monthly Report'].iloc[2])
for i in range(3,34):
column = f"Unnamed: {i}"
try:
day = (workbook[column].iloc[15])
year = month_[0:4]
month = month_[-2:]
value = (workbook[column].iloc[16])
output = (f"{int(day)}-{month}-{year},{value}")
with open('zonnecollectoren.csv', 'a') as f:
f.write(output)
f.write('\n')
# (f"{month}-{int(day)},{value}")
except:
pass
def main():
######################################################################
dir_name = r"C:\Users\rcxsm\Documents\python_scripts\in\zonnecollectoren_rznstrt_5"
to_do = "single_directory"
if to_do == "single_directory":
rootdir = dir_name
print(f"Browsing {dir_name}")
lengte = len(os.listdir(dir_name))
n = 1
os.chdir(dir_name) # change directory from working dir to dir with files
for file in os.listdir(dir_name): # loop through items in dir
file_name =dir_name + os.sep + file
do_your_thing(file_name)
elif to_do == "including_subdirectories":
rootdir = dir_name
for subdir, dirs, files in os.walk(dir_name):
for file in files:
do_your_thing(file)
else:
print("ERROR IN 'to_do' variable")
if __name__ == "__main__":
main()