在openpyxl中读取和写入大文件

时间:2017-07-26 09:11:57

标签: python pandas openpyxl

我正在尝试将单元格从大型excel文件复制到另一个大型excel文件,这两个文件都是.xlsx。我正在使用openpyxl模块。以下是我的代码:

>>> import openpyxl
>>> wb1=openpyxl.load_workbook('File1.xlsx',read_only=True)

错误:

Traceback (most recent call last):
  File "C:\Users\U6038633\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\reader\excel.py", line 118, in _validate_archive
    archive = ZipFile(filename, 'r', ZIP_DEFLATED)
  File "C:\Users\U6038633\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1108, in __init__
    self._RealGetContents()
  File "C:\Users\U6038633\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1175, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    wb2=openpyxl.load_workbook('Consolidated GL Line Item report_YTD.xlsx',read_only=True)
  File "C:\Users\U6038633\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\reader\excel.py", line 171, in load_workbook
    archive = _validate_archive(filename)
  File "C:\Users\U6038633\AppData\Local\Programs\Python\Python36-32\lib\site-packages\openpyxl\reader\excel.py", line 121, in _validate_archive
    archive = ZipFile(f, 'r', ZIP_DEFLATED)
  File "C:\Users\U6038633\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1108, in __init__
    self._RealGetContents()
  File "C:\Users\U6038633\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1175, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

在写入模式下打开时,我使用了代码:

wb2=openpyxl.load_workbook('File2.xlsx',write_only=True)

错误:

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    wb2=openpyxl.load_workbook('Consolidated GL Line Item report_YTD.xlsx',write_only=True)
TypeError: load_workbook() got an unexpected keyword argument 'write_only'

1 个答案:

答案 0 :(得分:0)

没有参数write_only=True

  

Docs » openpyxl package

load_workbook(filename, read_only=False, keep_vba=False, data_only=False, guess_types=False, keep_links=True)

使用:

wb2 = openpyxl.load_workbook('File2.xlsx')