1

I am currently trying to merge a number of excel spreadsheets into one workbook to create a monthly master workbook.

I have written the following code to try and achieve this:

...

from pathlib import Path
import xlwings as xw

print("enter file directory")
SOURCE_DIR = input()

excel_files = list(Path(SOURCE_DIR).glob("*.xlsx"))
combined_wb = xw.Book()

for excel_file in excel_files:
    wb = xw.Book(excel_files)
    for sheet in wb.sheets:
        sheet.api.copy(After=combined_wb.sheets[0].api)
    wb.close()

combined_wb.sheets[0].delete()
combined_wb.save(f("all_settlement_reports.xlsx"))

if len(combined_wb.app.books) == 1:
    combined_wb.app.quit()
else:
    combined_wb.close()

...

The first steps are fine, I am prompted for an input file path but then I get the following errors which have stumped me:

Traceback (most recent call last):
  File "C:\Users\Callum\Desktop\env\AutoSettle.py", line 11, in <module>
    wb = xw.Book(excel_files)
  File "C:\Users\Callum\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\main.py", line 817, in __init__
    fullname = fullname.lower()
AttributeError: 'list' object has no attribute 'lower'

Can anyone help out with this as I am really struggling to fix the problem.

Thanks everyone

gremur
  • 1,645
  • 2
  • 7
  • 20
Coakley
  • 13
  • 4
  • Replace `sheet.api.copy(After=combined_wb.sheets[0].api)`with `sheet.api.copy(None, After=combined_wb.sheets[0].api)`, otherwise it won't work properly. Maybe this could help you: https://stackoverflow.com/a/72006000/13968392 and https://stackoverflow.com/a/70022211/13968392 – mouwsy Apr 29 '22 at 13:50
  • Thanks for your comment. I have made this change but the error still arises. I have looked into these links but still can't make sense of the error I am getting. I am new to coding so sorry if I am making a really simple issue much bigger than it really is – Coakley Apr 29 '22 at 14:18

1 Answers1

0
from pathlib import Path

import xlwings as xw

SOURCE_DIR = input("enter file directory")

excel_files = list(Path(SOURCE_DIR).glob("*.xlsx"))
combined = xw.Book()

app = combined.app
app.interactive = False
app.visible = False

reference_sheet = combined.sheets[0]
for file_name in excel_files:
    for sheet in xw.Book(str(file_name)).sheets:
        sheet.copy(before=reference_sheet)

reference_sheet.delete()
combined.save("all_settlement_reports.xlsx")
app.kill()
Vovin
  • 720
  • 4
  • 16
  • This doesnt seem to work either. When I run it this way I get more errors that relate to sharepoint paths – Coakley Apr 29 '22 at 14:15
  • Thanks for this. I really appreciate the help. I am still getting this error. line 18, in for sheet in xw.Book(file_name): File "C:\Users\Callum\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\main.py", line 817, in __init__ fullname = fullname.lower() AttributeError: 'list' object has no attribute 'lower is there something wrong with my library? – Coakley Apr 29 '22 at 15:05
  • Hi Vovin. When i print excel files I get this [WindowsPath('C:/Users/Callum/Documents/Mar settlement/eetmobileunitedkingdom2022301.xlsx'), WindowsPath('C:/Users/Callum/Documents/Mar settlement/eeunitedkingdom2022301.xlsx') and it keeps going for all of the files in the folder. I am new to python so I may have missed something big but can this not work on a windows path? The files will be on my pc so I would need it to work from my computer. The new fix again did not work. Sorry for all the hassle and I really appreciate your help – Coakley Apr 29 '22 at 15:37
  • Yes I have copied and pasted the answer to ensure the correct code has been used. C:\Users\Callum\Desktop\env>AutoSettleV2.py enter file directoryC:/Users/Callum/Documents/Mar settlement Traceback (most recent call last): File "C:\Users\Callum\Desktop\env\AutoSettleV2.py", line 16, in for sheet in xw.Book(str(file_name)).sheets: File "C:\Users\Callum\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\main.py", line 823, in __init__ wb.fullname.lower() == fullname File – Coakley Apr 29 '22 at 16:02
  • "C:\Users\Callum\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\main.py", line 1074, in fullname return self.impl.fullname File "C:\Users\Callum\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\_xlwindows.py", line 837, in fullname return fullname_url_to_local_path( File – Coakley Apr 29 '22 at 16:02
  • "C:\Users\Callum\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\utils.py", line 566, in fullname_url_to_local_path return search_local_sharepoint_path( File "C:\Users\Callum\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\utils.py", line 705, in search_local_sharepoint_path raise xlwings.XlwingsError( xlwings.XlwingsError: Couldn't find your SharePoint file locally, see: xlwings.org/error this is the complete error code that comes up when I try to run it – Coakley Apr 29 '22 at 16:03
  • No the book doesn't open when I run that code – Coakley Apr 29 '22 at 16:20
  • File "C:\Users\Callum\Desktop\env\AutoSettle.py", line 12, in xw.Book(r"C:\Users\Callum\Documents\Mar settlement\eetmobileunitedkingdom2022301.xlsx") File "C:\Users\Callum\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\main.py", line 823, in __init__ wb.fullname.lower() == fullname – Coakley Apr 29 '22 at 16:24
  • I have just uninstalled and reintalled but the same errors are arising. I even set up a new environment and no luck. Really struggling to understand what I am doing wrong – Coakley Apr 29 '22 at 16:58
  • 1
    I now have it working. I installed xlwings addin as well and when I done this it worked. Didn't import anything new just worked after I installed the addin. From what I have read this shouldn't impact the code here but looks to have done the trick for some reason. Thank you so much for all of your help. I really appreciate it! – Coakley Apr 29 '22 at 17:35