Python Forum
Problem in if-else statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in if-else statement
#1
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 
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a problem with the IF Statement chlyde01 3 1,636 Nov-11-2024, 07:23 PM
Last Post: deanhystad
  problem with while statement. BobSmoss 3 3,075 Jan-08-2022, 03:22 PM
Last Post: BobSmoss
  multiple condition if statement problem FelixReiter 3 4,077 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  Problem with If statement and dataframe Milfredo 1 2,681 Sep-16-2020, 05:50 AM
Last Post: Milfredo
  Problem with If else statement Milfredo 5 4,265 Aug-30-2020, 06:32 PM
Last Post: Milfredo
  Problem with a 'break' statement. christopher3786 3 3,925 Jun-20-2020, 10:16 AM
Last Post: pyzyx3qwerty
  Problem with an IF statement Ryan_Todd 13 8,815 Jan-30-2020, 08:22 PM
Last Post: snippsat
  Problem with 'and' in 'if' statement CoderMan 3 4,218 Oct-06-2019, 07:32 PM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 4,512 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Problem with elif statement Haddal99 2 3,835 May-20-2019, 09:26 AM
Last Post: avorane

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.