Search code examples
csvxlsxpython-3.9

Converting xlsx to csv with pattern matching in Python not working


I have several xlsx files in directory. But i want to search xlsx file with filename 20230406115500.001.A0.XZI.INVOICING_ES101_Anlage_DISCO_Split_20230405_114751.xlsx.

I wanted to search above file with pattern matching something like *ES101*.xlsx and then convert this xlsx file into csv. There is only one file with this file name pattern. I am trying below python code:

import pandas as pd

read_file = pd.read_excel (r'/home/repo/"*ES101*.xlsx"')')
read_file.to_csv (r'/home/repo/ES101.csv', index = None, header=True)

But it throws error No such file or directory. I am running this python code in Linux server. I am using Python3.9 version.


Solution

  • You can just search with the glob function and then read the file:

    from glob import glob
    
    # it searches all the files containing the pattern, in this case I take the first and only one
    file_name = glob('/home/repo/*ES101*.xlsx')[0]
    
    dataframe = pd.read_excel(file_name, sheet_name='sheet_name')
    dataframe.to_csv("file_name.csv", index=False)
    
    # if you want the same name for the file you just can
    dataframe.to_csv(file_name.split('.')[0]+'.csv', index=False)
    

    You need to have the xlsxwriter engine installed.