Apr-08-2021, 06:15 AM
This code run only for .xlsx file but I want this general code that run for other excel format like(.xls) or(.xlsm) file or there is a combination of (.xls) and (.xlsm) file. Code is converting excel to csv file. Can anyone tell how I can do that??
from pathlib import Path
import time
import parser
import argparse
import pandas as pd
import os
import warnings
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser(description="Process some integers.")
parser.add_argument("path", help="define the directory to folder/file")
parser.add_argument("--verbose", help="display processing information")
start = time.time()
def main(path_xlsx, verbose):
if (".xlsx" in str(path_xlsx).lower()) and path_xlsx.is_file():
xlsx_files = [Path(path_xlsx)]
else:
xlsx_files = list(Path(path_xlsx).glob("*.xlsx"))
df = pd.DataFrame()
for fn in xlsx_files:
all_dfs = pd.read_excel(fn, sheet_name=None)
for sheet_name, df in all_dfs.items():
df = df.assign(DataSource=Path(fn.name))
x=os.path.splitext(fn.name)[0]
path=r'Output'
df.to_csv(os.path.join(path,f'{sheet_name}+{x}.csv'),index=False)
if __name__ == "__main__":
start = time.time()
args = parser.parse_args()
path = Path(args.path)
verbose = args.verbose
main(path, verbose) #Calling Main Function
print("Processed time:", time.time() - start) #Total Time
