以上程序报错:读取文件错误:Excel file format cannot be determined, you must specify an engine manually.
时间: 2023-09-21 15:10:19 浏览: 222
抱歉,Excel文件格式无法确定的错误可能是由于缺少适当的pandas的Excel读取引擎所致。你可以尝试使用openpyxl库来指定读取引擎。请尝试使用以下修改后的代码:
```python
import os
import csv
import pandas as pd
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.filechooser import FileChooserListView
class MainLayout(BoxLayout):
def __init__(self, **kwargs):
super(MainLayout, self).__init__(**kwargs)
self.orientation = 'vertical'
self.file_chooser = FileChooserListView()
self.add_widget(self.file_chooser)
self.select_button = Button(text='选择文件', size_hint=(1, 0.1))
self.select_button.bind(on_release=self.select_file)
self.add_widget(self.select_button)
self.read_button = Button(text='读取文件', size_hint=(1, 0.1))
self.read_button.bind(on_release=self.read_files)
self.add_widget(self.read_button)
self.result_label = Label(text='', size_hint=(1, 0.4))
self.add_widget(self.result_label)
self.save_button = Button(text='保存结果', size_hint=(1, 0.1))
self.save_button.bind(on_release=self.save_result)
self.add_widget(self.save_button)
self.exit_button = Button(text='退出', size_hint=(1, 0.1))
self.exit_button.bind(on_release=self.exit_app)
self.add_widget(self.exit_button)
def select_file(self, instance):
selected_file = self.file_chooser.selection and self.file_chooser.selection[0]
if selected_file:
self.selected_path = selected_file
def read_files(self, instance):
try:
files = [f for f in os.listdir(self.selected_path) if f.startswith('Cogging_') and not f.endswith('+.xlsx')]
result = ''
for file in files:
file_path = os.path.join(self.selected_path, file)
df = pd.read_excel(file_path, engine='openpyxl')
i17_value = df['I17'].values[0]
i7219_value = df['I7219'].values[0]
result += f'{file}: cogging-CW_pk_to_pk(abs): {i17_value}, cogging-CCW_pk_to_pk(abs): {i7219_value}\n'
self.result_label.text = result
except Exception as e:
self.result_label.text = f'读取文件错误: {str(e)}'
def save_result(self, instance):
try:
files = [f for f in os.listdir(self.selected_path) if f.startswith('Cogging_') and not f.endswith('+.xlsx')]
csv_path = os.path.join(self.selected_path, '筛选cogging.csv')
with open(csv_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['文件名', 'cogging-CW_pk_to_pk(abs)', 'cogging-CCW_pk_to_pk(abs)'])
for file in files:
file_path = os.path.join(self.selected_path, file)
df = pd.read_excel(file_path, engine='openpyxl')
i17_value = df['I17'].values[0]
i7219_value = df['I7219'].values[0]
writer.writerow([file, i17_value, i7219_value])
self.result_label.text = '保存成功'
except Exception as e:
self.result_label.text = f'保存失败: {str(e)}'
def exit_app(self, instance):
App.get_running_app().stop()
class MyApp(App):
def build(self):
return MainLayout()
if __name__ == '__main__':
MyApp().run()
```
请确保在运行代码之前已安装了openpyxl库:
```
pip install openpyxl
```
希望这次能够成功读取Excel文件并解决问题。如有其他问题,请随时提问!
阅读全文