Openpyxl ['MergedCell' object attribute 'hyperlink' is read-only] Problem - python

I met a problem while loading a xlsx File. In the worksheet there is a Hyperlink in a merged cell. While loading the file, error ocured. Can anybody
Code is just like this.
workbook = openpyxl.load_workbook(report_filepath)
Error Info:
File "F:\mainfunc_new.py", line 733, in read_report
workbook = openpyxl.load_workbook(report_filepath)
File "C:\Users\10225167\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\reader\excel.py", line 312, in load_workbook
reader.read()
File "C:\Users\10225167\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\reader\excel.py", line 274, in read
self.read_worksheets()
File "C:\Users\10225167\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\reader\excel.py", line 228, in read_worksheets
ws_parser.bind_all()
File "C:\Users\10225167\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\worksheet\_reader.py", line 389, in bind_all
self.bind_hyperlinks()
File "C:\Users\10225167\AppData\Local\Programs\Python\Python36\lib\site-packages\openpyxl\worksheet\_reader.py", line 355, in bind_hyperlinks
cell.hyperlink = link
AttributeError: 'MergedCell' object attribute 'hyperlink' is read-only
Thanks.

Use version 2.5.14 instead of yours. It worked for me.
pip install openpyxl==2.5.14

Double check if the cells that you're merging are empty. I don't think you can merge cells if that has values.

Maybe this error was caused by Excel format. I deleted the current excel file and replaced it with a good excel file, the error disappeared.

Related

Why am I getting a list out of range error while trying to save an excel workbook in python?

I'm trying to create a workbook,Edit it and save it.
Here is how the code looks :
cur_wb=openpyxl.Workbook()
-- Code modifying the values of cells.
Example : target_cell._style = copy(template_cell._style) --
filename="DB-"+months[inm]+".xlsx"
cur_wb.save(filename="output/"+filename)
File "Python\Python310\lib\site-packages\openpyxl\workbook\workbook.py", line 407, in save
save_workbook(self, filename)
File "Python\Python310\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook
writer.save()
File "Python\Python310\lib\site-packages\openpyxl\writer\excel.py", line 275, in save
self.write_data()
File "Python\Python310\lib\site-packages\openpyxl\writer\excel.py", line 84, in write_data
stylesheet = write_stylesheet(self.workbook)
File "Python\Python310\lib\site-packages\openpyxl\styles\stylesheet.py", line 253, in write_stylesheet
xf.alignment = wb._alignments[style.alignmentId]
IndexError: list index out of range
Why am I getting this error? Is it because I used copy?
So,
I just changed that one copy line of code.
I created a temporary NamedStyle, Copied the styles(Font,Alignment,Fill,Border) of the template cell to the NamedStyle and applied the Namedstyle to the target cell.
This fixed the issue.

Python pandas read_excel method KeyError 'show'

I am trying to read an excel File, extension .xlsx, with Pandas and it is displaying a KeyError: 'show'
This is the full error stack trace:
File "~/Python_VirtualEnv/api/lib/python3.7/site-packages/pandas/io/excel.py", line 653, in __init__
self._reader = self._engines[engine](self._io)
File "~/Python_VirtualEnv/api/lib/python3.7/site-packages/pandas/io/excel.py", line 424, in __init__
self.book = xlrd.open_workbook(filepath_or_buffer)
File "~/Python_VirtualEnv/api/lib/python3.7/site-packages/xlrd/__init__.py", line 138, in open_workbook
ragged_rows=ragged_rows,
File "~/Python_VirtualEnv/api/lib/python3.7/site-packages/xlrd/xlsx.py", line 812, in open_workbook_2007_xml
x12book.process_stream(zflo, 'Workbook')
File "~/Python_VirtualEnv/api/lib/python3.7/site-packages/xlrd/xlsx.py", line 271, in process_stream
meth(self, elem)
File "~/Python_VirtualEnv/api/lib/python3.7/site-packages/xlrd/xlsx.py", line 397, in do_sheet
bk._sheet_visibility.append(visibility_map[state])
KeyError: 'show'
If I open and re-save the file, then it reads perfectly fine so I am thinking the problem is with some formatting or Excel versioning that pandas is unable to read.
In my app, I can't ask the user to re-save so it needs to work right away. I have tested ~1000 files and there is only one that is giving this error.
What can I do to make sure the file is read when this error is thrown?
So the error you are having is happening because of an old error from xlrd.
The problem has been fixed in the Pull Request #323
Update the pandas package, the xlrd package and it should work again.

openpyxl load_workbook error: iteration over non sequence type

when i try to load_worbook (its a empty xlsx file) it get a error.
import openpyxl as op
wb = op.load_workbook(filename = 'C:\EXCEL TEST\TS.xlsx')
error
Runtime error (TypeErrorException): iteration over non-sequence of type Sequence
Traceback:
line 154, in find, "C:\RhinoPython\openpyxl\packaging\manifest.py"
line 163, in findall, "C:\RhinoPython\openpyxl\packaging\manifest.py"
line 142, in read_strings, "C:\RhinoPython\openpyxl\reader\excel.py"
line 269, in read, "C:\RhinoPython\openpyxl\reader\excel.py"
line 312, in load_workbook, "C:\RhinoPython\openpyxl\reader\excel.py"
line 14, in script
Any tip is welcome
You can try following sometimes its getting error because cells containing formulas not data. try following and see read_only=True is optional by the way
op.load_workbook(<file name>,data_only=True,read_only=True)
For further reading follow this link
Replace file path value C:\EXCEL TEST\TS.xlsx with C:\\EXCEL TEST\\TS.xlsx

Pandas open_excel() fails with xlrd.biffh.XLRDError: Can't find workbook in OLE2 compound document

I'm trying to use pandas to parse an .xlsm document. My code worked perfectly with the example file I was given, but once I got the rest of the documents, it failed with the above error. Here's the offending stack trace:
Traceback (most recent call last):
File "########/UnsupervisedCAM.py", line 9, in <module>
info_dict = read_excel_to_dict('files/' + filename)
File "########\readCAM.py", line 7, in read_excel_to_dict
df = pandas.read_excel(filename, parse_cols='E,G,I,K,Q,O')
File "########\Anaconda3\envs\tensorflow\lib\site-packages\pandas\io\excel.py", line 191, in read_excel
io = ExcelFile(io, engine=engine)
File "########\Anaconda3\envs\tensorflow\lib\site-packages\pandas\io\excel.py", line 249, in __init__
self.book = xlrd.open_workbook(io)
File "########\Anaconda3\envs\tensorflow\lib\site-packages\xlrd\__init__.py", line 441, in open_workbook
ragged_rows=ragged_rows,
File "########\Anaconda3\envs\tensorflow\lib\site-packages\xlrd\book.py", line 87, in open_workbook_xls
ragged_rows=ragged_rows,
File "########\Anaconda3\envs\tensorflow\lib\site-packages\xlrd\book.py", line 595, in biff2_8_load
raise XLRDError("Can't find workbook in OLE2 compound document")
xlrd.biffh.XLRDError: Can't find workbook in OLE2 compound document
I'm not even sure where to start... Haven't found anything of use online.
I got the same error message and could solve it by removing the password protection of the xlsx-file.
(not saying that it's the only reason for the error, but worth checking!)
After a lot of searching, the only way I've found to do this is to open and save all the excel documents, which seems to 'strip' them of their OLE2 format. I automated the process with the following vbs script:
Dim objFSO, objFolder, objFile
Dim objExcel, objWB
Set objExcel = CreateObject("Excel.Application")
Set objFSO = CreateObject("scripting.filesystemobject")
MyFolder = "<PATH/TO/FILES"
Set objFolder = objfso.getfolder(myfolder)
For Each objFile In objfolder.Files
If Right(objFile.Name,4) = "<EXTENSION>" Then
Set objWB = objExcel.Workbooks.Open(objFile)
objWB.save
objWB.close
End If
Next
objExcel.Quit
Set objExcel = Nothing
Set objFSO = Nothing
Wscript.Echo "Done"
Make sure to change the path to the folder and extension.
In case you face this issue over Jupyter notebook as I did when searching for the error, you can simply restart the kernel and the issue gets resolved.

Can't load xlsx file

I am trying to read attached xlsx (Click here to download ) file using python openpyxl. However, workbook cannot be loaded. Here is my attempt to open xlsx file in python -
>>> from openpyxl import load_workbook
>>> workbook = load_workbook(filename = "test.xlsx")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\openpyxl\reader\excel.py", line 136, in load_workbook
_load_workbook(wb, archive, filename, use_iterators, keep_vba)
File "C:\Python27\lib\site-packages\openpyxl\reader\excel.py", line 198, in _load_workbook
keep_vba=keep_vba)
File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 332, in read_worksheet
fast_parse(ws, xml_source, string_table, style_table, color_index)
File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 320, in fast_parse
parser.parse()
File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 137, in parse
dispatcher[tag_name](element)
File "C:\Python27\lib\site-packages\openpyxl\reader\worksheet.py", line 176, in parse_merge
self.ws.merge_cells(mergeCell.get('ref'))
File "C:\Python27\lib\site-packages\openpyxl\worksheet.py", line 815, in merge_cells
raise InsufficientCoordinatesException(msg)
openpyxl.shared.exc.InsufficientCoordinatesException: Range must be a cell range (e.g. A1:E1)
It appears that your .xlsx file is damaged or permanently corrupted. The reasons could be many. One of them could be that you might have renamed the extension of the file to .xlsx which would invalidate the file. To confirm this beahviour, please try to open this file in Microsoft Excel.
I tried reading the file through, openpyxl, xlrd and pandas but none of them worked.
>>> import xlrd
>>> xlrd.open_workbook('test.xlsx')
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '<html> <'
>>> from openpyxl import load_workbook
>>> workbook = load_workbook(filename = "test.xlsx")
InvalidFileException: File is not a zip file
>>> import pandas
>>> pandas.ExcelFile('test.xlsx')
InvalidFileException: File is not a zip file
I ran into this issue trying to open every file in a directory ending in *.xlsx .
I later found the file that caused the error was named ~$filename.xlsx . I'm guessing that Microsoft indicates that a file is currently opened by creating a file with the same name, prepended with the ~$. Once I closed the file, everything worked as expected.
The problem was that some merged cells were, in fact, merged with themselves. openpyxl expected a merged cell reference always to be a range of cells. A fix for the problem which ignores meaningless merges has been added to the 2.0 branch.
I like openpyxl and use it for creating xlsx documents. It could be a bug or a missing compatibility with excel feature that takes place in your specific document. I would report it to the openpyxl community
OK Guys.. I have reported this bug to openpyxl developers and they have provided a quick fix on this. Here is the complete thread.
I did never try openpyxl but I use xlrd for reading excel files (.xls and .xlsx). its work great.
see the examples and documentation at http://www.python-excel.org/

Categories

Resources