Questions icon indicating copy to clipboard operation
Questions copied to clipboard

Getting key error "if (saved_path != "") and (path is None): # Previously saved: Save under existing name self.xl.Save() KeyError: ''"

Open Babs958 opened this issue 1 year ago • 0 comments

I am running this code below to get to read multiple sheets in multiple excel files within a folder directory. But I keep running into this type error:

if (saved_path != "") and (path is None): Previously saved: Save under existing name self.xl.Save() KeyError: ''

Here is the code:

import os
import pandas as pd
import xlwings as xw

input_directory = 'C:/Users/BG database/test'
output_directory = 'C:/Users/BG database'
scenarios = os.path.join(output_directory, "output.xlsx")  
path2 = "renamed_" + input_directory

# Create a new Excel writer object
writer = pd.ExcelWriter(scenarios, engine='openpyxl')

while True:
   try:
     # List all files in the input directory
     files = os.listdir(input_directory)
     for file in files:
        if file.endswith(".xlsx"):
            excel_file_path = os.path.join(input_directory, file)
            excel_data = pd.read_excel(excel_file_path, sheet_name=None)
   except Exception as e:
     # Open the Excel file using xlwings
     wingsbook = xw.Book(excel_file_path)
     wingsapp = xw.apps.active
     # Save the Excel file with a new name
     wingsbook.save(path2)
     wingsapp.quit()
     # Update the path to point to the renamed file
     input_directory = path2
   else:
     break

You can find below the full traceback error that I keep getting:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File c:\Users\Programs\Python\Python311\Lib\site-packages\openpyxl\descriptors\base.py:59, in _convert(expected_type, value)
     58 try:
---> 59     value = expected_type(value)
     60 except:

TypeError: Fill() takes no arguments

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[30], line 20
     19             excel_file_path = os.path.join(input_directory, file)
---> 20             excel_data = pd.read_excel(excel_file_path, sheet_name=None)
     21 except Exception as e:
     22     # Open the Excel file using xlwings

File c:\Users\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_base.py:495, in read_excel(io, sheet_name, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, date_format, thousands, decimal, comment, skipfooter, storage_options, dtype_backend, engine_kwargs)
    494     should_close = True
--> 495     io = ExcelFile(
    496         io,
    497         storage_options=storage_options,
    498         engine=engine,
    499         engine_kwargs=engine_kwargs,
...
    870 if (saved_path != "") and (path is None):
    871     # Previously saved: Save under existing name
    872     self.xl.Save()

KeyError: ' '

Each time it gives this error, it then forces open one of the Excel files in the folder directory. Anyone has an idea what could be wrong?

Babs958 avatar Feb 15 '24 12:02 Babs958