I have a simple .xlsx excel file with no data. I want to write a value to a cell and save the workbook.
When I try using this code in Python 3.11.4:
import openpyxl
path = "C:\\Users\\abc\\Downloads\\Integration_Recon\\Output\\file.xlsx"
target_workbook = openpyxl.load_workbook(path)
target_sheet = target_workbook['Sheet1']
target_sheet.cell(row=2, column=2, value = "111111")
target_workbook.save(path)
I get an error that says:
Traceback (most recent call last):
File "c:\Users\abc\Downloads\Integration_Recon\Code\tst2.py", line 6, in <module>
target_workbook.save(path)
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\workbook\workbook.py", line 386, in save
save_workbook(self, filename)
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\writer\excel.py", line 294, in save_workbook
writer.save()
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\writer\excel.py", line 275, in save
self.write_data()
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\writer\excel.py", line 77, in write_data
self._write_worksheets()
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\writer\excel.py", line 215, in _write_worksheets
self.write_worksheet(ws)
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\writer\excel.py", line 200, in write_worksheet
writer.write()
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\worksheet\_writer.py", line 361, in write
self.close()
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\worksheet\_writer.py", line 369, in close
self.xf.close()
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\worksheet\_writer.py", line 289, in get_stream
with xf.element("worksheet", xmlns=SHEET_MAIN_NS):
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 144, in __exit__
next(self.gen)
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\et_xmlfile\xmlfile.py", line 50, in element
self._write_element(el)
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\site-packages\et_xmlfile\xmlfile.py", line 77, in _write_element
xml = tostring(element)
^^^^^^^^^^^^^^^^^
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\xml\etree\ElementTree.py", line 1098, in tostring
ElementTree(element).write(stream, encoding,
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\xml\etree\ElementTree.py", line 731, in write
with _get_writer(file_or_filename, encoding) as (write, declared_encoding):
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 137, in __enter__
return next(self.gen)
^^^^^^^^^^^^^^
File "C:\Users\abc\AppData\Local\Programs\Python\Python311\Lib\xml\etree\ElementTree.py", line 794, in _get_writer
file = io.TextIOWrapper(file,
^^^^^^^^^^^^^^^^^^^^^^
LookupError: unknown encoding: us-ascii
Why does this happen, and how can I fix it? I've tried looking through online links but couldn't figure out the issue. May be I can try through pandas but I have a large part of code written in openpyxl so dont want to try other module.