Python Forum
Need help in understanding this particular Traceback TypeError
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help in understanding this particular Traceback TypeError
#1
Hi guys, this is my first post and my apologies in advance if I missed out something.

I'm learning about Python and doing web scraping with regex then get the results and export it to an Excel file with Pandas. At first, I was using VS Code then switched to Jupyter Lab. There were no code errors, but once I run my .py file I get this strange error that I had never seen before and I can't find it with Google too.

The strange part is on Windows, I'm able to successfully export to Excel although it still throws the same error but on a Mac, it will still be exported but I can't open it and Excel says the file is corrupted.

Huh

Error:
Traceback (most recent call last): File "ph_gold_scrape.py", line 53, in <module> gold_price_df.to_excel("gold_price_scrape.xlsx", sheet_name="Sheet1") File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 2256, in to_excel engine=engine, File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/formats/excel.py", line 742, in write writer.save() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/excel/_openpyxl.py", line 44, in save return self.book.save(self.path) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/workbook/workbook.py", line 408, in save save_workbook(self, filename) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/writer/excel.py", line 293, in save_workbook writer.save() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/writer/excel.py", line 275, in save self.write_data() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/writer/excel.py", line 75, in write_data self._write_worksheets() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/writer/excel.py", line 215, in _write_worksheets self.write_worksheet(ws) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/writer/excel.py", line 200, in write_worksheet writer.write() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/worksheet/_writer.py", line 354, in write self.write_top() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/worksheet/_writer.py", line 98, in write_top self.write_properties() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/worksheet/_writer.py", line 60, in write_properties self.xf.send(props.to_tree()) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/openpyxl/worksheet/_writer.py", line 294, in get_stream xf.write(el) File "src/lxml/serializer.pxi", line 1652, in lxml.etree._IncrementalFileWriter.write TypeError: got invalid input value of type <class 'xml.etree.ElementTree.Element'>, expected string or Element
Reply
#2
Hi Dear
I am beginner but I got same problem from a few days ago.
I think I update anaconda and openpyxl version(3.0.2) and then this problem begin to happen.
but xlswwriter works well.
I solve this problem from downgrade openpyxl version(2.6.3)
after downgrade, this message gone and work properly.
down grade process
1. pip uninstall openpyxl
2. pip install openpyxl==2.6.3
3. after computer restart , everything is ok
(PS) I did downgrade only openpyxl.
I hope you can solve this problem.
Reply
#3
Thank you for your help. I will check out your solution when I have some extra time, currently I managed a workaround by exporting the DataFrame with
.to_csv()
and it works without errors.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output, TypeError and traceback error fier259 2 3,098 May-06-2018, 10:52 PM
Last Post: fier259

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020