报错如下

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Users\Admin\Anaconda3\envs\tf2\lib\site-packages\pandas\core\generic.py", line 2196, in to_excel
    storage_options=storage_options,
  File "D:\Users\Admin\Anaconda3\envs\tf2\lib\site-packages\pandas\io\formats\excel.py", line 826, in write
    freeze_panes=freeze_panes,
  File "D:\Users\Admin\Anaconda3\envs\tf2\lib\site-packages\pandas\io\excel\_openpyxl.py", line 414, in write_cells
    xcell.value, fmt = self._value_with_fmt(cell.val)
  File "D:\Users\Admin\Anaconda3\envs\tf2\lib\site-packages\openpyxl-3.0.7-py3.7.egg\openpyxl\cell\cell.py", line 215, in value
    self._bind_value(value)
  File "D:\Users\Admin\Anaconda3\envs\tf2\lib\site-packages\openpyxl-3.0.7-py3.7.egg\openpyxl\cell\cell.py", line 194, in _bind_value
    value = self.check_string(value)
  File "D:\Users\Admin\Anaconda3\envs\tf2\lib\site-packages\openpyxl-3.0.7-py3.7.egg\openpyxl\cell\cell.py", line 162, in check_string
    raise IllegalCharacterError
openpyxl.utils.exceptions.IllegalCharacterError

解决办法:

  1. 安装模块:pip install xlsxwriter

  2. 修改代码

# 原代码:df.to_excel('abc.xlsx')
# 现将代码修改如下:
import xlsxwriter
df.to_excel('abc.xlsx', engine='xlsxwriter')

参考链接

posted on 2021-08-03 18:09  jaysonteng  阅读(2867)  评论(0编辑  收藏  举报