使用openpyxl无法读取Excel文件 [英] Can't read excel files, using openpyxl

查看:2087
本文介绍了使用openpyxl无法读取Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有最后一行相似的excel文件列表.它包含有关客户的私人信息(他的名字,姓氏,电话).每个excel文件都对应一个客户端.我需要使用每个客户端的所有数据制作一个excel文件.我决定自动执行此操作,因此转到了openpyxl库.我编写了以下代码,但无法正常工作.

I have a list of excel files with similar last row. It contains private information about client (his name, surname, phone). Each excel file corresponds to a client. I need to make one excel file with all data about every client. I decide to do it automatically, so looked to openpyxl library. I wrote the following code, but it doesn't work correctly.

import openpyxl
import os
import glob
from openpyxl import load_workbook
from openpyxl import Workbook
import openpyxl.styles
from openpyxl.cell import get_column_letter

path_kit = 'prize_input/kit'

#creating single document
prize_info = Workbook()
prize_sheet = prize_info.active

file_array_reciever = []

for file in glob.glob(os.path.join(path_kit, '*.xlsx')):
    file_array_reciever.append(file)

row_num = 1
for f in file_array_reciever:
    f1 = load_workbook(filename=f)
    sheet = f1.active
    for col_num in range (3, sheet.max_column):
        prize_sheet.cell(row=row_num, column=col_num).value = \
            sheet.cell(row=sheet.max_row, column=col_num).value

    prize_info.save("Ex.xlsx")

我收到此错误:

Traceback (most recent call last):
  File "/Users/zkid18/PycharmProjects/untitled/excel_test.py", line 43, in <module>
    f1 = load_workbook(filename=f)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/openpyxl/reader/excel.py", line 183, in load_workbook
    wb.active = read_workbook_settings(archive.read(ARC_WORKBOOK)) or 0
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/zipfile.py", line 1229, in read
    with self.open(name, "r", pwd) as fp:
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/zipfile.py", line 1252, in open
    zinfo = self.getinfo(name)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/zipfile.py", line 1196, in getinfo
    'There is no item named %r in the archive' % name)
KeyError: "There is no item named 'xl/workbook.xml' in the archive"

好像是读取文件时出现问题.
我不知道档案文件中哪里有一个名为'xl/workbook.xml'的项目.

Looks like it is a problem with reading file.
I don't understand where it gets an item named 'xl/workbook.xml' in the archive.

推荐答案

根据您使用的版本,这可能是openpyxl中的错误.例如,在1.6.1中引入了一个表现出这种行为的错误.恢复到1.5.8修复了它.根据此openpyxl 门票;尽管票证上没有说明修复程序的交付时间,但它是在2013年初提交的.我升级到1.6.2,错误消失了.

Depending on which version you are using, this could be a bug in openpyxl. For example, in 1.6.1 a bug was introduced exhibiting this behavior. Reverting to 1.5.8 fixed it. There was a fix according to this openpyxl ticket; though the ticket doesn't say when the fix was delivered, it was committed in early 2013. I upgraded to 1.6.2 and the error went away.

这篇关于使用openpyxl无法读取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆