0

So I've been trying to code a script which loads all excel files from a specific location and moves worksheets inside these files into one workbook. I'm ending with and error:

AttributeError: 'DataFrame' object has no attribute 'DataFrame'.

I'm pretty new to this so I would really appreciate any tip on how to make that work. I can stick only with openpyxl because at the moment I cannot install xlrd module on my workstation.

from pandas import ExcelWriter
import glob
import pandas as pd
import openpyxl

writer = ExcelWriter("output.xlsx")
for filename in glob.glob (r"C:\path\*.xlsx"):
    wb = openpyxl.load_workbook(filename)
    for ws in wb.sheetnames:
        ws = wb[ws]
        print (ws)
        data = ws.values
        columns = next(data)[0:]
        df= pd.DataFrame(data, columns=columns)
        print(df)
        for df in df.DataFrame:
            df.to_excel([writer,sheet_name= ws)

writer.save()
Michał
  • 5
  • 5
  • Does this answer your question? [How to concatenate three excels files xlsx using python?](https://stackoverflow.com/questions/15793349/how-to-concatenate-three-excels-files-xlsx-using-python) – Tomerikoo Mar 01 '20 at 19:11
  • It didn't help me out with that. – Michał Mar 01 '20 at 19:33

1 Answers1

0

first you have to use sheet_name as a string not an object and another thing is last for loop is not needed as we loop through sheet names.

from pandas import ExcelWriter
import glob
import pandas as pd
import openpyxl


writer = ExcelWriter("output.xlsx")
for filename in glob.glob (r"C:\path\*.xlsx"):
    wb = openpyxl.load_workbook(filename)
    for ws in wb.sheetnames:
        ws1 = wb[ws]
        data = ws1.values
        columns = next(data)[0:]
        df= pd.DataFrame(data, columns=columns)
        df.to_excel(writer,sheet_name=ws,index = False)

writer.save()
Jay Kakadiya
  • 501
  • 1
  • 5
  • 12