Skip to content

Instantly share code, notes, and snippets.

@mrprofessor
Created December 31, 2019 08:57
Show Gist options
  • Save mrprofessor/5f9e1f93a35fa657ad4abf859c8db642 to your computer and use it in GitHub Desktop.
Save mrprofessor/5f9e1f93a35fa657ad4abf859c8db642 to your computer and use it in GitHub Desktop.
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, fileformat):
name = file_split(inputfile)
try:
os.makedirs(name)
except:
pass
df1 = pd.ExcelFile(inputfile)
for x in df1.sheet_names:
print(x + "." + fileformat, "Done!")
df2 = pd.read_excel(inputfile, sheet_name=x)
filename = os.path.join(name, x + "." + fileformat)
if fileformat == "csv":
df2.to_csv(filename, index=False)
else:
df2.to_excel(filename, index=False)
print("\nAll Done!")
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument("inputfile")
@click.option(
"-f",
"--format",
type=click.Choice(["xlsx", "csv"]),
default="xlsx",
help="Default xlsx.",
)
def cli(inputfile, format):
"""Convert a Excel file with multiple sheets to several file with one sheet.
Examples:
\b
getsheets filename
\b
getsheets filename -f csv
"""
if format == "csv":
getsheets(inputfile, "csv")
else:
getsheets(inputfile, "xlsx")
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment