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
Related
I am using xlwings in python to read excel file and add my dataframe in excel file. Earlier my code was working fine but since yesterday its giving error as below :
OSError: [WinError -2147467259] Unspecified error
Code:
app = xw.App(visible=False)
wb = xw.Book(file_path)
ws = wb.sheets[1]
ws.range(A2).options(index=False).value = df
wb.save()
wb.close()
app.quit()
Error is coming on the second line of code where it tries to open the book.
wb = xw.Book(file_path)
Complementary to moken's comment: Using with xw.App(visible=False) as app: (available since Version 0.24.3) is more stable than app = xw.App(visible=False), because it ensures that everything is properly cleaned up again and to prevent zombie processes after an error. Here is an example:
import xlwings as xw
path = r"test.xlsx"
with xw.App(visible=False) as app:
wb = xw.Book(path)
ws = wb.sheets[1]
ws.range("A2").options(index=False).value = df
wb.save(path)
wb.close()
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
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
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.
I am trying to open and format a specific Excel worksheet. However I am having trouble trying to find out how to look at a specific worksheet.
The code I'm trying to use to open the workbook and go to a specific worksheet and then change the font for specific cells is:
from openpyxl import Workbook
def applyValidations(path,tabname):
workbook = Workbook(path)
worksheet = workbook[tabname]
c = worksheet['A1:A5']
c.font = Font(size=22)
The error I'm getting is:
KeyError: 'Worksheet Department Data does not exist.'
Department Data is the name of the worksheet which does exist in the workbook.
Here's some code I use regularly with openpyxl, which may solve your Q:
from openpyxl import load_workbook
wb = load_workbook(filename=data_file, read_only=True)
ws = wb.active
print(wb.sheetnames)