forked from hrjp/flipcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elsx2csv.py
71 lines (59 loc) · 1.74 KB
/
elsx2csv.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
from datetime import date
import numpy as np
import matplotlib.pyplot as plt
import openpyxl
import os
import pandas as pd
import unicodedata
import re
import csv
#ファイルのパス
input_file_path=os.path.dirname(__file__)+"/xlsx/cafeteria_schedule.xlsx"
output_file_path=os.path.dirname(__file__)+"/csv/"
# 実数に変換できるか判定
def is_num(s):
try:
float(s)
except ValueError:
return False
else:
return True
# シートをcsvに変換して出力
def excel_to_csv(seat):
# dataframeに変換
df = pd.DataFrame(seat.values)
#営業時間部分を抽出
df2=df.iloc[4:,[3,9]]
for j in range(2):
i=0
while not(df2.iloc[i,j] is None):
#for k in range(30):
if df2.iloc[i,j]=="休業":
df2.iloc[i,j]="-1:-1~-1:-1"
else:
#全角文字を半角に変換
text=df2.iloc[i,j]
df2.iloc[i,j]=unicodedata.normalize("NFKC", text)
i+=1
df2=df2.head(i)
out_list=[]
for k in range(len(df2)):
out_list.append(re.split('[:~]',df2.iloc[k,0])+re.split('[:~]',df2.iloc[k,1]))
df3=pd.DataFrame(out_list)
return df3
#エクセルファイルロード
wb = openpyxl.load_workbook(input_file_path)
for sheet in wb.sheetnames:
if is_num(sheet):
year=int(re.split('[.]',sheet)[0])
month=int(re.split('[.]',sheet)[1])
#シート選択
ws = wb[sheet]
if month<10:
decade="0"
else:
decade=""
file_path=output_file_path+str(year)+decade+str(month)+".csv"
#csvに変換して出力
excel_to_csv(ws).to_csv(file_path, index = False,header=False)
print("xlsx --> csv converted")