from openpyxl import load_workbook
exl_1=load_workbook(filename='test.xlsx')print(exl_1.sheetnames)
---------------------------------------------------------------------------
BadZipFile Traceback (most recent call last)
<ipython-input-8-b3bf9383ac19> in <module>
1 from openpyxl import load_workbook
----> 2 exl_1=load_workbook(filename='test.xlsx')
3 print (exl_1.sheetnames)
~/opt/anaconda3/lib/python3.8/site-packages/openpyxl/reader/excel.py in load_workbook(filename, read_only, keep_vba, data_only, keep_links)
313
314 """
--> 315 reader = ExcelReader(filename, read_only, keep_vba,
316 data_only, keep_links)
317 reader.read()
~/opt/anaconda3/lib/python3.8/site-packages/openpyxl/reader/excel.py in __init__(self, fn, read_only, keep_vba, data_only, keep_links)
122 def __init__(self, fn, read_only=False, keep_vba=KEEP_VBA,
123 data_only=False, keep_links=True):
--> 124 self.archive = _validate_archive(fn)
125 self.valid_files = self.archive.namelist()
126 self.read_only = read_only
~/opt/anaconda3/lib/python3.8/site-packages/openpyxl/reader/excel.py in _validate_archive(filename)
94 raise InvalidFileException(msg)
95
---> 96 archive = ZipFile(filename, 'r')
97 return archive
98
~/opt/anaconda3/lib/python3.8/zipfile.py in __init__(self, file, mode, compression, allowZip64, compresslevel, strict_timestamps)
1267 try:
1268 if mode == 'r':
-> 1269 self._RealGetContents()
1270 elif mode in ('w', 'x'):
1271 # set the modified flag so central directory gets written
~/opt/anaconda3/lib/python3.8/zipfile.py in _RealGetContents(self)
1334 raise BadZipFile("File is not a zip file")
1335 if not endrec:
-> 1336 raise BadZipFile("File is not a zip file")
1337 if self.debug > 1:
1338 print(endrec)
BadZipFile: File is not a zip file
sheet=exl_1['work']
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-7-8fe1271ea388> in <module>
----> 1 sheet=exl_1['work']
NameError: name 'exl_1' is not defined
sheet=exl_1.active
print(sheet.dimensions)#获取内容大小
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-33e04b2089ff> in <module>
----> 1 sheet=exl_1.active
2 print (sheet.dimensions)#获取内容大小
NameError: name 'exl_1' is not defined
from openpyxl import load_workbook
exl=load_workbook('test.xlsx')
sheet=exl.active
for row in sheet.iter_rows(min_row=1,max_row=100,
min_col=1,max_col=20):for cell in row:ifnot cell.value:print(cell.coordinate)
---------------------------------------------------------------------------
BadZipFile Traceback (most recent call last)
<ipython-input-63-bb2be71e8dff> in <module>
1 from openpyxl import load_workbook
----> 2 exl=load_workbook('test.xlsx')
3 sheet=exl.active
4 for row in sheet.iter_rows(min_row=1,max_row=100,
5 min_col=1,max_col=20):
~/opt/anaconda3/lib/python3.8/site-packages/openpyxl/reader/excel.py in load_workbook(filename, read_only, keep_vba, data_only, keep_links)
313
314 """
--> 315 reader = ExcelReader(filename, read_only, keep_vba,
316 data_only, keep_links)
317 reader.read()
~/opt/anaconda3/lib/python3.8/site-packages/openpyxl/reader/excel.py in __init__(self, fn, read_only, keep_vba, data_only, keep_links)
122 def __init__(self, fn, read_only=False, keep_vba=KEEP_VBA,
123 data_only=False, keep_links=True):
--> 124 self.archive = _validate_archive(fn)
125 self.valid_files = self.archive.namelist()
126 self.read_only = read_only
~/opt/anaconda3/lib/python3.8/site-packages/openpyxl/reader/excel.py in _validate_archive(filename)
94 raise InvalidFileException(msg)
95
---> 96 archive = ZipFile(filename, 'r')
97 return archive
98
~/opt/anaconda3/lib/python3.8/zipfile.py in __init__(self, file, mode, compression, allowZip64, compresslevel, strict_timestamps)
1267 try:
1268 if mode == 'r':
-> 1269 self._RealGetContents()
1270 elif mode in ('w', 'x'):
1271 # set the modified flag so central directory gets written
~/opt/anaconda3/lib/python3.8/zipfile.py in _RealGetContents(self)
1334 raise BadZipFile("File is not a zip file")
1335 if not endrec:
-> 1336 raise BadZipFile("File is not a zip file")
1337 if self.debug > 1:
1338 print(endrec)
BadZipFile: File is not a zip file
from openpyxl import load_workbook
exl=load_workbook(filename='test.xlsx')#载入工作簿
sheet=exl.active#激活工作表
sheet['A1']='hello world'
exl.save(filename='test.xlsx')
---------------------------------------------------------------------------
BadZipFile Traceback (most recent call last)
<ipython-input-64-b521c601b73b> in <module>
1 from openpyxl import load_workbook
----> 2 exl=load_workbook(filename='test.xlsx')#载入工作簿
3 sheet=exl.active#激活工作表
4 sheet['A1']='hello world'
5 exl.save(filename='test.xlsx')
~/opt/anaconda3/lib/python3.8/site-packages/openpyxl/reader/excel.py in load_workbook(filename, read_only, keep_vba, data_only, keep_links)
313
314 """
--> 315 reader = ExcelReader(filename, read_only, keep_vba,
316 data_only, keep_links)
317 reader.read()
~/opt/anaconda3/lib/python3.8/site-packages/openpyxl/reader/excel.py in __init__(self, fn, read_only, keep_vba, data_only, keep_links)
122 def __init__(self, fn, read_only=False, keep_vba=KEEP_VBA,
123 data_only=False, keep_links=True):
--> 124 self.archive = _validate_archive(fn)
125 self.valid_files = self.archive.namelist()
126 self.read_only = read_only
~/opt/anaconda3/lib/python3.8/site-packages/openpyxl/reader/excel.py in _validate_archive(filename)
94 raise InvalidFileException(msg)
95
---> 96 archive = ZipFile(filename, 'r')
97 return archive
98
~/opt/anaconda3/lib/python3.8/zipfile.py in __init__(self, file, mode, compression, allowZip64, compresslevel, strict_timestamps)
1267 try:
1268 if mode == 'r':
-> 1269 self._RealGetContents()
1270 elif mode in ('w', 'x'):
1271 # set the modified flag so central directory gets written
~/opt/anaconda3/lib/python3.8/zipfile.py in _RealGetContents(self)
1334 raise BadZipFile("File is not a zip file")
1335 if not endrec:
-> 1336 raise BadZipFile("File is not a zip file")
1337 if self.debug > 1:
1338 print(endrec)
BadZipFile: File is not a zip file
import xlwt
workbook=xlwt.Workbook(encoding='utf-8')
sheet=workbook.add_sheet('my worksheet')
sheet.write(1,0,label='this is test')
workbook.save('new_test.xls')