Python Forum

Full Version: Problem in if-else statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
        
You specifically filter only xlsx files. Change your code to process also xls file.
Note for xls files you will need to instal xlrd package in addition to openpyxl.
https://pandas.pydata.org/docs/reference...read_excel
(Apr-08-2021, 07:40 AM)buran Wrote: [ -> ]You specifically filter only xlsx files. Change your code to process also xls file.
Note for xls files you will need to instal xlrd package in addition to openpyxl.
https://pandas.pydata.org/docs/reference...read_excel

Can you show what changes I made in this code that it works for other excel file thing. Because some of files are .xls,.xlsx &.xlsm