Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lilloraffa
Forked from scottming/getsheets.py
Created October 16, 2018 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lilloraffa/6b74a5e8542d626712b15964514a3477 to your computer and use it in GitHub Desktop.
Save lilloraffa/6b74a5e8542d626712b15964514a3477 to your computer and use it in GitHub Desktop.
# -*- 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 + '.xlsx')
df2.to_excel(filename, index=False)
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment