Python Forum
Problem in if-else statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem in if-else statement (/thread-33232.html)



Problem in if-else statement - shantanu97 - Apr-08-2021

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
        



RE: Problem in if-else statement - buran - Apr-08-2021

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/api/pandas.read_excel.html#pandas.read_excel


RE: Problem in if-else statement - shantanu97 - Apr-09-2021

(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/api/pandas.read_excel.html#pandas.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