在openpyxl中从zipfolder打开Excel文件

时间:2017-01-10 11:25:19

标签: python openpyxl

我正在尝试使用代码。

from zipfile import ZipFile
from openpyxl import load_workbook
from io import BytesIO

zip_path = r"path/to/zipfile.zip"
with ZipFile(zip_path) as myzip:
    with myzip.open(myzip.namelist()[0]) as myfile:
        wb = load_workbook(filename=BytesIO(myfile.read()))
        data_sheet = wb.worksheets[1]
        for row in data_sheet.iter_rows(min_row=3, min_col=3):
            print(row[0].value)

显示

  

ValueError:stat:路径对于Windows来说太长了

这可能吗?

我正在尝试来自Using openpyxl to read file from memory

的逻辑

1 个答案:

答案 0 :(得分:0)

使用xlrd以下代码可以正常工作。

with ZipFile(zip_path) as myzip:
        with myzip.open(myzip.namelist()[0]) as myfile:
            book = xlrd.open_workbook(file_contents=(myfile.read()))
    sh = book.sheet_by_index(0)
    #your code here