How to save excel workbook in normal mode with openpyxl? - python

I am opening an Excel with openpyxl library, however it is saving the Excel in read - only mode
I tried the answer of this question:
Openpyxl does not close Excel workbook in read only mode
wb._archive.close()
but it give me AttributeError: 'Workbook' object has no attribute '_archive'.
I am working with the following code:
wb = openpyxl.Workbook()
sheet1 = wb.create_sheet("mysheet", 0)
sheet1 = wb["mysheet"]
sheet1.cell(row=1, column=1).value = '123'
sheet1.cell(row=1, column=2).value = 'summary'
wb.save(filename) /*filename has the adress of xlsx*/
The Excel file is created without problem, however it is in 'read only' mode. How can I prevent this? Is there an option for the save or create method to avoid read only mode?
Thanks for your help.

I suspect the problem is simply one of file permissions in Windows. Read-only mode in openpyxl have nothing to do with this and is not relevant here. Check the permissions and ownership of the file you've created and that it is not opened by any other program or process.

Related

How to unfreeze excel column using python script

How to unfreeze excel column using python script
wb = load_workbook(excel_file)
ws = wb.active
ws.freeze_panes = None
wb.save(excel_file)
Using above code unfreeze is happening, however If you open processed excel file, the below error is prompted
"we found a problem with some content in excel_file.xlsx. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes"
Use the code below to avoid this error: "we found a problem with
some content in excel_file.xlsx. Do you want us to try to recover as
much as we can? If you trust the source of this workbook, click Yes"
ws - worksheet, wb - workbook
ws.sheet_view.selection = [ws.sheet_view.selection[0]]
ws.sheet_view.selection[0].pane = 'topLeft'
wb.save(excel_file)

open excel file on sheet1 after changes with xlwings

I need an Excel workbook to open on sheet1 or by that sheets name. I'm using xlwings to alter a workbook with several sheets. Everything works fine but when the file is opened in Excel it opens on the last sheet that has been altered. I can't seem to find an answer. Please help. Thanks
This is what might help you
# load Workbook
workbook= xw.Book('path_file')
#acivate sheet by name
sheet = workbook.sheets.activate('Sheet_Name')
if that doesn't work try
sheet = workbook.sheets["Sheet_Name"]

Update .xlsm file values python

I have a .xlsm file as a reference template. I want to update the values of this .xlsm file using python from a .csv file.
template .xlsm ----> Update values using .csv
What has not worked :
I tried using pandas.to_excel method. but the .xlsm file gets corrupted after I write to sheet.
Could someone please point me in the right direction ?
openpyxl supports xlsm file.
from openpyxl import load_workbook
wb2 = load_workbook('test.xlsm', keep_vba=True)
update(wb2, csvfile.csv) # this is where you need to work according to your need.
wb.save('new_document.xlsm')
wb.close()
https://openpyxl.readthedocs.io/en/default/tutorial.html
Maybe to try xlwings, using it something like this?
def update(workbook, csv_file):
ws = workbook.sheets[2]
ws.range('B14').value = 155
from xlwings import Book
wb = Book(r'test.xlsm')
update(wb, csv_file)
wb.save('test1.xlsm')
wb.close()
This is the best tool to update xlsm files since it uses WindowsAPI and macros are triggered in case something is changed. This means, it won't work on Linux.
Of course, update function must do something more meaningful than changing the B14 cell in the 3rd sheet.
For more info, please read http://docs.xlwings.org/en/stable/quickstart.html

Using openpyxl to edit a spreadsheet. Cannot write to cells, "cell is read-only error"

I'm trying to modify an excel worksheet in Python with Openpyxl, but I keep getting an error that says the cells are read only. I know for a fact that they are not because I've been editing these spreadsheets manually for the past two months and have had no issues. Does anyone have an idea of what might be happening? I'm just trying to get my bearings on editing sheets with openpyxl so it is basic code.
rpt = file
workbook = openpyxl.load_workbook(filename = os.path.join('./Desktop/',rpt), use_iterators = True) # Tells which wb to open
wb=workbook
#worksheets = wb.get_sheet_names()
ws = wb.active
ws['A1'] = 42
Any help will be greatly appreciated. Thanks!
Thanks for the responses, to clarify, I'm not getting a workbook is read only error, it is specifically referring to the cells. I'm not sure what's causing this since I know that the workbook is not a read only workbook. Should I be using a different excel library for python? Is there a more robust excel library?
Thanks!
You are opening the workbook in read-only mode which is why the cells are read-only.
In case any other desperate soul is searching for a solution:
As stated in an answer here if you pass use_iterators = True the returned workbook will be read-only.
In newer versions of openpyxl the use_iterators was renamed to read_only, e.g.:
import openpyxl
rpt = file
workbook = openpyxl.load_workbook(filename = os.path.join('./Desktop/',rpt), read_only = True) # Tells which wb to open
wb=workbook
#worksheets = wb.get_sheet_names()
ws = wb.active
ws['A1'] = 42
Will yield:
TypeError: 'ReadOnlyWorksheet' object does not support item assignment
So in order to do the modification you should use read_only = False.

Data not present in excel sheet

I'm reading a existing excel file by using openpyxl package and trying to save that file it, and it got saved but after opening that excel file no data is present. I used the following code and my requirement is to open the file in use_iterators = True mode only
from openpyxl import load_workbook
wb = load_workbook(filename = 'large_file.xlsx', use_iterators = True)
ws = wb.get_sheet_by_name(name = 'big_data')
for row in ws.iter_rows():
for cell in row:
print cell.internal_value
wb.save("large_file.xlsx")
can u guys show how to save the file and close the file after saving with out losing the data
Try loading with use_iterators = False, as use_iterators = True loads the data information differently, such that it may not contain all the information you wish to save.
Openpyxl writes and entirely new excel file based on the information it has read in, so it's not like you make a small change and just update the file. (This also means if certain features aren't supported in openpyxl (such as VB macros), these won't exist in the file you've saved.)

Categories