简体   繁体   中英

Python Error: AttributeError: 'NoneType' object has no attribute 'to_excel'

I am trying to combine all files in a directory and the save the combined file into another directory. I am using Python 3.8. When I run the code I get the following with a AttributeError:

c:\test\Upload test\Book1.xlsx
c:\test\Upload test\Book2.xlsx
c:\test\Upload test\Book3.xlsx
Traceback (most recent call last):
File "C:/Python/PythonDev/combine.py", line 104, in <module>
newdf.to_excel(writer,"All")
AttributeError: 'NoneType' object has no attribute 'to_excel'

the code:

import pandas as pd
import globe
filelist = glob.glob(r'c:\test\Upload test\*.xlsx')
file1 = "*.*"
for i in  filelist:
    file2 = pd.read_excel(i)
    file2['FileName'] = i
    file1 = ['newdf']
    newdf = file1.append(file2)
    print (i)
writer = pd.ExcelWriter(r'c:\test\Uploaded\uploadfile.xlsx', engine= 'xlsxwriter')
newdf.to_excel(writer,"All")
writer.save()

Append doesn't return anything...

Try something like this:

import pandas as pd
import glob

raw_files = glob.glob(r'c:\test\Upload test\*.xlsx')

pd_files = pd.DataFrame()

for file in raw_files:
    pd_files.append(pd.read_excel(file))

pd_files.to_excel("c:\test\Uploaded\uploadfile.xlsx")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM