鱼C论坛

 找回密码
 立即注册
查看: 77|回复: 1

批量合并Excel失败问题求助

[复制链接]
发表于 2024-5-30 09:15:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

import pandas as pd

import os

data_list = []
for fname in os.listdir("."):
    if fname.startswith("收货查询0") and fname.endswith(".xls"):
        data_list.append(pd.read_excel(fname))  

data_all = pd.concat(data_list)  
data_all.to_excel("合并.xls",index=False)


如上代码,为什么我在执行的时候提示错误,我初步感觉是因为我的模块有问题,好像无法识别 .xls,但是不知道对不对,也不知道怎么修正,求帮忙,具体错误如下:


D:\Program Files\PYTHON\lib\site-packages\openpyxl\styles\stylesheet.py:226: UserWarning: Workbook contains no default style, apply openpyxl's default
  warn("Workbook contains no default style, apply openpyxl's default")
D:/wangxuewen19/Downloads/合并/批量合并Excel.py:12: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to 'xlwt' to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.
  data_all.to_excel("合并.xls",index=False)
Traceback (most recent call last):
  File "D:/wangxuewen19/Downloads/合并/批量合并Excel.py", line 12, in <module>
    data_all.to_excel("合并.xls",index=False)
  File "D:\Program Files\PYTHON\lib\site-packages\pandas\core\generic.py", line 2345, in to_excel
    formatter.write(
  File "D:\Program Files\PYTHON\lib\site-packages\pandas\io\formats\excel.py", line 888, in write
    writer = ExcelWriter(  # type: ignore[abstract]
  File "D:\Program Files\PYTHON\lib\site-packages\pandas\io\excel\_xlwt.py", line 39, in __init__
    import xlwt
ModuleNotFoundError: No module named 'xlwt'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-5-30 09:15:20 | 显示全部楼层
错误提示中提到了两个问题,一个是关于openpyxl的警告,另一个是关于xlwt模块的错误。

关于openpyxl的警告,它是由于你的Excel文件中没有默认样式所引起的。你可以忽略这个警告,或者通过设置openpyxl的默认样式来解决。具体的做法是,在导入pandas之前加入以下代码:
import openpyxl
openpyxl.styles.builtins.default = None

关于xlwt模块的错误,它是因为你使用的pandas版本使用的是openpyxl引擎,而不是xlwt引擎。xlwt引擎已经不再维护,因此在将数据保存到xls格式时会出错。为了解决这个问题,你需要安装openpyxl模块,并将"合并.xls"改成"合并.xlsx",即将文件扩展名从xls改为xlsx。代码修改如下:
import pandas as pd
import os
import openpyxl

openpyxl.styles.builtins.default = None

data_list = []
for fname in os.listdir("."):
    if fname.startswith("收货查询0") and fname.endswith(".xls"):
        data_list.append(pd.read_excel(fname))

data_all = pd.concat(data_list)
data_all.to_excel("合并.xlsx", index=False)

这样就可以顺利地将数据合并并保存为xlsx格式的文件了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-26 12:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表