xls to xlsx in new folder-done but have error mesage

274 Views Asked by At

I have a large number of xls and xlsx files and i want to have a copy all of them in a new folder and in xlsx format ... I make it this(have all files in new folder with xlsx format) but in the end i take error in terminal ....Why this hapend? how can fix that ?

import os


here_path= os.getcwd()

final_directory = os.path.join(here_path, r'all_xlsx')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
   

import pandas as pd

for r, d, f in os.walk(here_path):                  
    for file in f:
        if '.xls' in file:
            df = pd.read_excel(file)
            print(df)
            
            
            name_fil=os.path.splitext(file)[0]
            
            writer = pd.ExcelWriter(final_directory+"/"+name_fil+".xlsx", engine='xlsxwriter')                  
            
            df.to_excel(writer, sheet_name='Sheet1')
            
            writer.save()         

                

the error is :

[1599 rows x 29 columns]
Traceback (most recent call last):
  File "a2_all_to_xlsx_folder.py", line 38, in <module>
    df = pd.read_excel(file)
  File "/usr/local/lib/python3.8/dist-packages/pandas/util/_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_base.py", line 867, in __init__
    self._reader = self._engines[engine](self._io)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_xlrd.py", line 22, in __init__
    super().__init__(filepath_or_buffer)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_base.py", line 353, in __init__
    self.book = self.load_workbook(filepath_or_buffer)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_xlrd.py", line 37, in load_workbook
    return open_workbook(filepath_or_buffer)
  File "/home/kubdim/.local/lib/python3.8/site-packages/xlrd/__init__.py", line 111, in open_workbook
    with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: '1_ëÇÆ_Åä11 öôæêëåæ Çéùéåæ (çäæå 113)_éäî (ïä äïÅ.)_üÅ.xlsx'
1

There are 1 best solutions below

5
On

Try this:

df = pd.read_excel(file, encoding=sys.getfilesystemencoding())

or: encoding='utf-8'