如何解决在新文件夹中将xls转换为xlsx,但出现错误消息
我有大量的xls和xlsx文件,我想将它们全部复制到一个新文件夹中,并采用xlsx格式... 我做到了这一点(所有文件都以xlsx格式保存在新文件夹中),但最终我在终端机中出错...为什么会这样?如何解决呢?
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()
错误是:
[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'
解决方法
尝试一下:
df = pd.read_excel(file,encoding=sys.getfilesystemencoding())
或:encoding ='utf-8'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。