-
Notifications
You must be signed in to change notification settings - Fork 2
/
get-sheets.py
executable file
·60 lines (43 loc) · 1.26 KB
/
get-sheets.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import click
import os
import pandas as pd
def file_split(file):
s = file.split('.')
name = '.'.join(s[:-1]) # get directory name
return name
def getsheets(inputfile):
name = file_split(inputfile)
try:
os.makedirs(name)
except:
pass
df1 = pd.ExcelFile(inputfile)
for x in df1.sheet_names:
print(x + '.xlsx', 'Done!')
df2 = pd.read_excel(inputfile, sheetname=x)
filename = os.path.join(name, x.lower() + '.csv')
df2.to_csv(filename, index=False, encoding='utf-8')
print('\nAll Done!')
def get_sheet_names(inputfile):
df = pd.ExcelFile(inputfile)
for i, flavor in enumerate(df.sheet_names):
print('{0:>3}: {1}'.format(i + 1, flavor))
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.command(context_settings=CONTEXT_SETTINGS)
@click.option('-n', '--sheet-names', is_flag=True)
@click.argument('inputfile')
def cli(sheet_names, inputfile):
'''Convert a Excel file with multiple sheets to several file with one sheet.
Examples:
\b
getsheets filename
\b
getsheets -n filename
'''
if sheet_names:
get_sheet_names(inputfile)
else:
getsheets(inputfile)
cli()