Error with load_workbook function in Python - python

I am getting a BadZipFile error while using the load_workbook function.
PANEL_PATH = "C:/Test/panel_ph.xlsx"
wb = Workbook()
wb.save(PANEL_PATH)
panel_writer = pd.ExcelWriter(PANEL_PATH, engine='openpyxl')
panel_writer.book = load_workbook(PANEL_PATH,read_only=False)
Can someone please suggest how to resolve it.
Thanks,
Nitin

Related

how to delete all the data in a sheet in excel?

from openpyxl import load_workbook
wb = load_workbook("C:\op.xlsx")
ws = wb.active
This is the code I have so far as I don't know what to do next so pls help me.
Try:
ws.delete_rows(1, ws.max_row)
You could also delete the sheet and create another one:
del wb[wb.active.title]
wb.create_sheet()
See
https://openpyxl.readthedocs.io/en/stable/api/openpyxl.worksheet.worksheet.html#openpyxl.worksheet.worksheet.Worksheet.delete_rows
https://openpyxl.readthedocs.io/en/stable/api/openpyxl.workbook.workbook.html#openpyxl.workbook.workbook.Workbook.create_sheet

python - pandas with openpyxl damages my workbook

Using pandas with the openpyxl engine I want to open an excel woorkbook and add a sheet.
Because, the sheets are growing and getting quit big I want to create a new sheet without reading all the other sheet.
So instead of xlsxwriter, I did start using openpyxl, however I get the following issue that the file gets damaged and needs to recovered by excel.
After that, when I run it a second time, I get the python error that the excel file is 'raise BadZipFile("File is not a zip file")'.
This my test code:
import openpyxl
import pandas
from pandas import ExcelWriter
from pandas import ExcelFile
from openpyxl import Workbook
from openpyxl import load_workbook
sheet1 = 'sheet1'
sheet2 = 'sheet2'
if os.path.exists(filename):
workbook = openpyxl.load_workbook(filename)
else:
workbook = Workbook()
writer = pandas.ExcelWriter(filename, engine='openpyxl')
writer.book = workbook
df_1.to_excel(writer, sheet_name=sheet1)
writer.save()
writer.close()
time.sleep(2) # to give it some time. But doesn't help :(
if os.path.exists(filename):
workbook = openpyxl.load_workbook(filename)
else:
workbook = Workbook()
writer = pandas.ExcelWriter(filename, engine='openpyxl')
writer.book = workbook
df_2.to_excel(writer, sheet_name=sheet2)
writer.save()
writer.close()
Any suggestions how to solve this? Or do I miss something?
btw, excel 365 - 16.46 macOS

Openpyxl is not saving the excel file, why?

here is my code:
import openpyxl as op
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl import worksheet
path = ...
op.load_workbook(path)
wb = Workbook()
ws2 = wb.create_sheet('Sheet2')
wb.save(filename = 'NameOfTheFile.xlsx')
Now everything works, except saving the file, when I add:
print(wb.sheetnames)
it does print out the Sheet2 I just added, but for some reason when I go to the documents where the file is, it does not update it, that sheet is nowhere to be found.

Insert image in existing excel sheet by using xlsxwriter

i am trying to add image into the existing excel sheet by using xlsxwriter module
import xlsxwriter
workbook = xlsxwriter.Workbook('C:/Users/Desktop/blank.xlsx')
worksheet = workbook.get_worksheet_by_name('Sheet1')
worksheet.insert_image('B5', 'C:/Users/Desktop/CaseDeatails/Abc.jpg')
i am getting the below error
Traceback (most recent call last):
File "C:\Users\Desktop\insertImage.py", line 23, in
worksheet.insert_image('B5', 'C:/Users/Desktop/CaseDeatails/Abc.jpg')
AttributeError: 'NoneType' object has no attribute 'insert_image'
Please help me on this error
inseart image in xlsxwriter
import xlsxwriter
import os
workbook = xlsxwriter.Workbook('C:/Users/Desktop/blank.xlsx')
worksheet = workbook.get_worksheet_by_name('Sheet1')
image = os.path.join(settings.BASE_DIR, "C:/Users/Desktop/CaseDeatails/", "Abc.jpg")
worksheet.insert_image('B5', image)
That isn't possible with XlsxWriter since it cannot read or modify an existing file.
Try the OpenPyXL module instead.
Also for some reason python doesn't like the links to be:
C:/Users/Desktop/blank.xlsx
They have to have a double /, so it should be:
C://Users//Desktop//blank.xlsx
It is not a xlsxwriter solution, but it works well:
from openpyxl import Workbook
from openpyxl.drawing.image import Image
wb = Workbook()
sheet1 = wb.create_sheet('sheet1',0)
active = wb['sheet1']
active.add_image(Image('fig.png'),'A1')
wb.save('myfile.xlsx')

Why is the below Python program showing error?

On executing the below code it shows error
load_workbook() got an unexpected keyword argument 'read_only'
from openpyxl import load_workbook
from openpyxl import Workbook
wb = load_workbook(filename="textxl.xlsx", read_only = True)
wb2 = Workbook()
print wb.get_sheet_names()
wb2 = wb
wb2.save('balances.xlsx')
I want to know the reason

Categories