D:\work\cs\python\sqlite>python openxls.py
SyntaxError: Non-UTF-8 code starting with '\xd0' in file D:\work\cs\python\sqlite\openxls.py on line 4, but no encoding declared; see https://peps.python.org/pep-0263/ for details
解决方案, 将 openxls.py 带签名的 UTF-8 编码存储.
解决这个问题后再运行, 出现错误, 未找到 openpyxl 模块.
D:\work\cs\python\sqlite>python openxls.py
Traceback (most recent call last):
File "D:\work\cs\python\sqlite\openxls.py", line 3, in
import openpyxl
ModuleNotFoundError: No module named 'openpyxl'
模块可以通过 pip 安装. pip 本身就是python的一个模块.
使用 pip 列出安装的python模块, 如果找不到pip, 则执行
python -m ensurepip
安装一下.
D:\work\cs\python\sqlite>pip list
Package Version
---------- -------
pip 24.0
setuptools 65.5.0
[notice] A new release of pip is available: 24.0 -> 24.2
[notice] To update, run: python.exe -m pip install --upgrade pip
那就升级一下吧
D:\work\cs\python\sqlite>python.exe -m pip install --upgrade pip
Requirement already satisfied: pip in d:\python\python311\lib\site-packages (24.0)
Collecting pip
Downloading pip-24.2-py3-none-any.whl.metadata (3.6 kB)
Downloading pip-24.2-py3-none-any.whl (1.8 MB)
------------------------------------- 1.8/1.8 MB 4.1 MB/s eta 0:00:00
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 24.0
Uninstalling pip-24.0:
Successfully uninstalled pip-24.0
Successfully installed pip-24.2
D:\work\cs\python\sqlite>pip list
Package Version
---------- -------
pip 24.2
setuptools 65.5.0
安装 openpyxl 模块
D:\work\cs\python\sqlite>pip install openpyxl
Collecting openpyxl
Downloading openpyxl-3.1.5-py2.py3-none-any.whl.metadata (2.5 kB)
Collecting et-xmlfile (from openpyxl)
Downloading et_xmlfile-1.1.0-py3-none-any.whl.metadata (1.8 kB)
Downloading openpyxl-3.1.5-py2.py3-none-any.whl (250 kB)
Downloading et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)
Installing collected packages: et-xmlfile, openpyxl
Successfully installed et-xmlfile-1.1.0 openpyxl-3.1.5
然后执行 python
出现错误
openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support the old .xls file format, please use xlrd to read this file, or convert it to the more recent .xlsx file format.
原来是openpyxl不支持旧版的 .xls 文件格式, 可将 .xls 文件转换为 .xlsx 格式, 或者使用 xlrd 读取该文件.