找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2953|回复: 3

[求助] 从网上参考的首个python程序,基本上是依样画葫芦,不行啊.

4

主题

9

帖子

9

积分

贫民

积分
9
minman 发表于 2022-5-29 21:49:57 | 显示全部楼层 |阅读模式
将当前某个目录下所有Excel文件合并(纵向)到一个excel文件中程序如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import pandas as pd

def get_files(path):
    fs = []
    for root, dirs, files in os.walk(path):
        for file in files:
            file_name = file.split(".")[0]
            file_type = file.split(".")[1]
            if file_type in ["xls", "xlsx"] and not file_name[0:2] == "合并":
                fs.append(os.path.join(root, file))
    print(fs)
    return fs


def hebing(path,sc_filename):
    # 批量表所在文件夹路径,指定为当前目录下的xlsdata目录
    files = get_files(path)
    arr = []
    for i in files:
        print(i)
        arr.append(pd.read_excel(i))
        # 目标文件路径为当前目录
        writer = pd.read_excelwriter(sc_filename)
        pd.concat(arr).to_excel(writer, 'sheet1', index=False)
        writer.save()
    return


if __name__ == '__main__':
    xlsfiledir = './xlsdata/'
    sc_filename = "./xlsdata合并文件.xlsx"
    hebing(xlsfiledir, sc_filename)

运行时错误提示如下:
Traceback (most recent call last):
  File "/Users/jiankunlou/PycharmProjects/test/合并多个同类xls文件.py", line 36, in <module>
    hebing(xlsfiledir, sc_filename)
  File "/Users/jiankunlou/PycharmProjects/test/合并多个同类xls文件.py", line 25, in hebing
    arr.append(pd.read_excel(i))
  File "/Users/jiankunlou/PycharmProjects/test/venv/lib/python3.7/site-packages/pandas/util/_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)
  File "/Users/jiankunlou/PycharmProjects/test/venv/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "/Users/jiankunlou/PycharmProjects/test/venv/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 867, in __init__
    self._reader = self._engines[engine](self._io)
  File "/Users/jiankunlou/PycharmProjects/test/venv/lib/python3.7/site-packages/pandas/io/excel/_xlrd.py", line 22, in __init__
    super().__init__(filepath_or_buffer)
  File "/Users/jiankunlou/PycharmProjects/test/venv/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 353, in __init__
    self.book = self.load_workbook(filepath_or_buffer)
  File "/Users/jiankunlou/PycharmProjects/test/venv/lib/python3.7/site-packages/pandas/io/excel/_xlrd.py", line 37, in load_workbook
    return open_workbook(filepath_or_buffer)
  File "/Users/jiankunlou/PycharmProjects/test/venv/lib/python3.7/site-packages/xlrd/__init__.py", line 170, in open_workbook
    raise XLRDError(FILE_FORMAT_DESCRIPTI**[file_format]+'; not supported')
xlrd.biffh.XLRDError: Excel xlsx file; not supported

Process finished with exit code 1




请问错在哪里?该怎么解决?

回复

使用道具 举报

0

主题

9

帖子

9

积分

贫民

积分
9
jiangshuhengxin 发表于 2022-7-29 10:37:10 | 显示全部楼层
引入pandas,需要另外安装第三方模块,如openpyxl等可以打开excel的应用
回复 支持 反对

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
victory610 发表于 2022-8-15 15:15:01 | 显示全部楼层
xlrd.biffh.XLRDError: Excel xlsx file; not supported
已经提醒你了呀,xlsx根本就解析不了,你需要安装所需的包。
回复 支持 反对

使用道具 举报

4

主题

9

帖子

9

积分

贫民

积分
9
minman  楼主| 发表于 2022-11-14 11:45:35 | 显示全部楼层
总算弄明白了。我这excel文件,扩展名的确是xls,但其实是以html格式存储的xls文件。所以pd.read_excel是打不开这一类文件的。所以问题变成了:如何用pandas打开以html格式存储的xls文件?
回复 支持 反对

使用道具 举报

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

本版积分规则

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