file_content = pkg_resources.resource_string(__name__, "../Remote_file/遥控器码值.xlsx")这个报了openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support b'.xmlpk\x05\x06\x00\x00\x00\x00\x13\x00\x13\x00\x93\x04\x00\x00\x934\x00\x00\x00\x00' file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm
时间: 2023-08-22 19:08:47 浏览: 41
这个错误提示和你之前提到的错误提示是一样的,都是说 openpyxl 不支持打开 b'.xmlpk\x05\x06\x00\x00\x00\x00\x13\x00\x13\x00\x93\x04\x00\x00\x934\x00\x00\x00\x00' 这种文件格式,建议先用 Excel 打开文件确认一下是否可用。openpyxl 支持的文件格式是 .xlsx, .xlsm, .xltx, .xltm。你可以尝试将文件转换成 openpyxl 支持的格式再进行打开。如果你确认文件格式正确,仍然无法打开,可以考虑使用其他 Python 库来打开该文件,比如 pandas 库的 read_excel 函数。
相关问题
stream = pkg_resources.resource_stream(__name__, file)
这行代码使用了Python中的pkg_resources模块的resource_stream函数,用于获取当前包中指定文件的文件流对象。具体来说,这个函数的第一个参数指定包名,通常使用__name__,表示当前模块的包名;第二个参数指定要获取的文件名。函数的返回值是一个文件流对象,可以像使用普通文件一样读取其中的内容。
例如,如果当前模块所在的包中包含一个名为test.txt的文件,可以使用下面的代码获取它的文件流对象:
```
import pkg_resources
file_stream = pkg_resources.resource_stream(__name__, 'test.txt')
```
这个代码将获取当前包中的test.txt文件,并返回一个文件流对象file_stream。接下来可以使用Python的文件操作函数,如read、readline、readlines等读取文件中的内容。注意,使用完文件流对象后,应该显式地关闭它,以释放资源:
```
content = file_stream.read()
file_stream.close()
```
pyinstaller打包出来的exe文件报FileNotFoundError: [Errno 2] No such file or directory: '../Remote_file/遥控器码值.xlsx'
这个问题通常是因为PyInstaller默认不会将代码中的资源文件打包进可执行文件中,导致程序在运行时无法找到这些资源文件。解决这个问题的方法有以下几种:
1. 使用绝对路径访问资源文件
可以使用os.path.abspath()函数将相对路径转换为绝对路径,例如:
```
import os
file_path = os.path.abspath("../Remote_file/遥控器码值.xlsx")
```
这里将相对路径"../Remote_file/遥控器码值.xlsx"转换为绝对路径后存储在变量file_path中,可以在代码中使用该变量来访问文件。
2. 手动将资源文件打包进可执行文件中
可以使用PyInstaller提供的--add-data参数将资源文件手动添加到可执行文件中。具体操作方法请参考我之前回答的问题。
3. 在代码中使用pkg_resources包
如果您的资源文件是Python包的一部分,可以使用Python内置的pkg_resources包来访问这些资源文件。例如:
```
import pkg_resources
file_content = pkg_resources.resource_string(__name__, "../Remote_file/遥控器码值.xlsx")
```
这里使用pkg_resources.resource_string()函数读取了相对路径"../Remote_file/遥控器码值.xlsx"指定的文件内容,并存储在变量file_content中。需要注意的是,使用pkg_resources需要将资源文件作为包的一部分打包,否则仍然无法访问该文件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)