File is not a zip file - python

I try to open a file with openpyxl but only get the error:
raise BadZipFile("File is not a zip file") zipfile.BadZipFile: File is not a zip file
A simple code example:
from openpyxl import load_workbook
wb = load_workbook('path.xlsx')
Context: I am trying to open an existing excel file, edit the file and then save it. Until now I am able to only read the file with pandas.read_excel(path.xlsx).

The excel files were in read-only mode. I saved the file as a new file and load_workbook worked.

Related

how to open a text file after opening workbook in openpyxl Python

i have been trying to append some data on my excel sheet using openpyxl and after that append some data in my text file...for some reason it gives me error whenever i try to open my text file
import openpyxl
from openpyxl import*
path = "students.xlsx"
To open the workbook
wb = openpyxl.load_workbook(path)
Get workbook active sheet object
sheet = wb.active
data to later append
data = (
(1, "john", 1/5/2022)
)
appending data
sheet.append(data)
saving and closing xlsx file
wb.save("students.xlsx")
wb.close()
problem lies here
with open("students.txt", "a") as f:
f.write(data[0]+data[1] + data[2])
here's my error
Traceback (most recent call last):
File "D:\taha\tools\attendance\quiz.py", line 32, in <module>
with open("students.txt", "a") as f:
File "C:\Users\FLH\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
reader = ExcelReader(filename, read_only, keep_vba,
File "C:\Users\FLH\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
self.archive = _validate_archive(fn)
File "C:\Users\FLH\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 94, in _validate_archive
raise InvalidFileException(msg)
openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support .txt file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm
In a quick view, you defined data as a tuple that contains a tuple with three elements.
As a first step to solve should use a list instead a tuple of tuples:
data = [1, "john", 1/5/2022]
Could you try this and let me know if it solves or you need more help.
Saying “thanks” is appreciated, but it doesn’t answer the question. Instead, vote up the answers that helped you the most! If these answers were helpful to you, please consider saying thank you in a more constructive way – by contributing your own answers to questions your peers have asked here.

Python openpyxl telling me " No such file or directory:" even though python file is in same directory as the excel file, among others

Beginner here.
I've tried making new excel files, running the python code while the excel file is open, tried renaming the excel file, but nothing works. I was just following this youtube tutorial: https://www.youtube.com/watch?v=7YS6YDQKFh0&t=14s
from openpyxl import Workbook, load_workbook
workbook = load_workbook("Test2.xlsx")
The error:
Traceback (most recent call last):
File "c:\Users\Name\Desktop\Python Projects\Excel Manipulation\Excel_Test.py", line 3, in <module>
workbook = load_workbook("Test2.xlsx")
File "C:\Users\Name\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
reader = ExcelReader(filename, read_only, keep_vba,
File "C:\Users\Name\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
self.archive = _validate_archive(fn)
File "C:\Users\Name\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
archive = ZipFile(filename, 'r')
File "C:\Users\Name\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1239, in __init__
self.fp = io.open(file, filemode)
**FileNotFoundError: [Errno 2] No such file or directory: 'Test2.xlsx'
PS C:\Users\Name>**
I found a solution. Turns out you just need to copy the file location, convert it to raw string, and add \ExcelFile to the end:
from openpyxl import Workbook, load_workbook
location = r'C:\Users\Name\Desktop\Python Projects\Excel Manipulation\Test2.xlsx'
workbook = load_workbook(location)
Derp.

Python Writing .csv file [duplicate]

I can't save the active file I'm working in when I'm in openpyxl.
wb_obj = load_workbook(filename="C:\\Users\\timde\PycharmProjects\\starshipit\\test.xlsx", read_only=False)
sheet_obj = wb_obj.active
sheet_obj.cell(row=2, column=10).value = 500
wb_obj.save("test.xlsx")
I get this error back
File "C:/Users/timde/PycharmProjects/starshipit/writeback_to_sheet.py", line 22, in <module>
write_back()
File "C:/Users/timde/PycharmProjects/starshipit/writeback_to_sheet.py", line 15, in write_back
wb_obj.save("test.xlsx")
File "C:\Users\timde\PycharmProjects\starshipit\venv\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
save_workbook(self, filename)
File "C:\Users\timde\PycharmProjects\starshipit\venv\lib\site-packages\openpyxl\writer\excel.py", line 291, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Users\timde\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1251, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'test.xlsx'
But if I change the file name to test1.xlsx It creates a new file and saves it for me.
So the issue is only saving the current workbook I am in as itself
Thank you all in advance. Much appreciated
MS Office applications generally write-lock the files that they open. Since you have your workbook open in Excel, Python will not be able to open the same workbook. This manifests itself as the PermissionError that you are seeing. The simple solution is to close the file in Excel when you want to use it elsewhere.

Error while loading excel file with Openpyxl

I am trying yo load an excel file using Openpyxl in Python.
from openpyxl import load_workbook
wb2 = load_workbook('Book1.xlsx')
print wb2.get_sheet_names()
It just these three lines. and it throws the following error:
Traceback (most recent call last):
File "C:/Python27/excel1.py", line 5, in <module>
wb2 = load_workbook('Book1.xlsx')
File "C:\Python27\Lib\site-packages\openpyxl\reader\excel.py", line 141, in load_workbook
archive = ZipFile(f, 'r', ZIP_DEFLATED)
File "C:\Python27\Lib\zipfile.py", line 793, in __init__
self._RealGetContents()
File "C:\Python27\Lib\zipfile.py", line 835, in _RealGetContents
raise BadZipfile, "File is not a zip file"
BadZipfile: File is not a zip file
This is exactly like their Documentation. https://openpyxl.readthedocs.org/en/latest/tutorial.html
Is their any better package to do this.
If this file does not exist at the desired location, try:
from openpyxl import load_workbook
from openpyxl import Workbook
# 1) create a workbook
wb = Workbook()
wb.save('my.xlsx')
del wb
# 2) build connection with the just created excel
book = load_workbook('my.xlsx')
It is an issue with the file. When we saved the file externally it was prompting this error. easy fix is to make a try catch and if the it gives the error create a new file in the same name and save on the same place. But for appending with old data wont work in this case.
try:
# this statement shall raise error for Badzip file
wb_obj = openpyxl.load_workbook(filename=path)
except:
# try to create a new file and save at same path
wb_obj = openpyxl.Workbook()
wb_obj.save(path)
wb_obj = openpyxl.load_workbook(filename=path)
xlsx files are zip archives. It looks like the file you're trying to open isn't. If you think this isn't the case please submit a but with a sample file.

xlrd cannot read xlsx file downloaded from email attachment

This is a very very strange issue. I have quite a large excel file (the contents of which I cannot discuss as it is sensitive data) that is a .xlsx and IS a valid excel file.
When I download it from my email and save it on my desktop and try to open the workbook using xlrd, xlrd throws an AssertionError and does not show me what went wrong.
When I open the file using my file browser, then save it (without making any changes), it works perfectly with xlrd.
Has anyone faced this issue before? I tried passing in various flags to the open_workbook function to no avail and I tried googling for the error. So far I haven't found anything.
The method I used was as follows
file = open('bigexcelfile.xlsx')
fileString = file.read()
wb = open_workbook(file_contents=filestring)
Please help! The error is as follows
Traceback (most recent call last):
File "./varify/samples/resources.py", line 354, in post
workbook = xlrd.open_workbook(file_contents=fileString)
File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/__init__.py", line 416, in open_workbook
ragged_rows=ragged_rows,
File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/xlsx.py", line 791, in open_workbook_2007_xml
x12sheet.process_stream(zflo, heading)
File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/xlsx.py", line 528, in own_process_stream
self_do_row(elem)
File "/home/vagrant/varify-env/lib/python2.7/site-packages/xlrd/xlsx.py", line 722, in do_row
assert tvalue is not None
AssertionError
rename or Save as your Excel file as .xls instead of .xlsx
Thank You
Use pyopenxl, not xlrd, for this format: https://openpyxl.readthedocs.org/en/latest/

Categories