Anonymous
Ошибка «элемент последовательности 2: ожидаемый экземпляр str, найден NoneType» при работе формата HDF5 в Python
Сообщение
Anonymous » 17 янв 2025, 20:58
Я пишу программу, которая преобразует многостраничные файлы Excel в формат .h5. Программа нормально работает на Python, но после упаковки в exe-файл с помощью pyinstaller функция Convert_to_hdf5 сообщает об ошибке «элемент последовательности 2: ожидаемый экземпляр str, найден NoneType».
Код: Выделить всё
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd
import numpy as np
from pathlib import Path
import threading
import os
from os.path import expanduser
from datetime import datetime
class ExcelToHDF5Converter:
def __init__(self, root):
self.root = root
self.root.title("Excel转HDF5")
self.folder_path = ""
self.setup_ui()
def setup_ui(self):
self.center_window(350, 180)
# set window
root.configure(background='#f0f0f0')
self.convert_button = tk.Button(self.root, text="Select a Excel File", width=30, command=self.start_convert, padx=10, pady=5)
self.convert_button.pack(pady=20)
self.open_path_button = tk.Button(self.root, text="open folder", width=30, command=self.open_path, padx=10, pady=5)
self.open_path_button.pack(pady=20)
self.open_path_button.config(state=tk.DISABLED)
def center_window(self, width, height):
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 3) - (height / 2)
self.root.geometry('%dx%d+%d+%d' % (width, height, x, y))
def start_convert(self):
file_path = filedialog.askopenfilename(filetypes=[("Excel文件", "*.xlsx *.xls")])
if not file_path:
# messagebox.showinfo("message", "select excel file")
return
self.convert_button.config(text="Converting, please wait...", state=tk.DISABLED)
threading.Thread(target=self.convert_to_hdf5, args=(file_path,)).start()
def convert_to_hdf5(self, file_path):
error_message = '' # error_message
downloads_path = os.path.join(expanduser("~"), 'Downloads')
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
original_path = Path(file_path)
new_filename = f"{original_path.stem}-{current_time}"
hdf5_filename = Path(new_filename).with_suffix('.h5')
hdf5_path = Path(downloads_path) / hdf5_filename
try:
xls = pd.ExcelFile(file_path)
with pd.HDFStore(hdf5_path, 'w') as hdf:
for sheet_name in xls.sheet_names:
df = pd.read_excel(xls, sheet_name=sheet_name, dtype=str)
df.fillna("", inplace=True) # replace NaN to ""
df = df.astype(str)
safe_sheet_name = sheet_name.replace(' ', '_').replace('-', '_')
if df.shape[0] < 3: # if data line
Подробнее здесь: [url]https://stackoverflow.com/questions/78241851/error-sequence-item-2-expected-str-instance-nonetype-found-while-operating-h[/url]
1737136737
Anonymous
Я пишу программу, которая преобразует многостраничные файлы Excel в формат .h5. Программа нормально работает на Python, но после упаковки в exe-файл с помощью pyinstaller функция Convert_to_hdf5 сообщает об ошибке «элемент последовательности 2: ожидаемый экземпляр str, найден NoneType». [code]import tkinter as tk from tkinter import filedialog, messagebox import pandas as pd import numpy as np from pathlib import Path import threading import os from os.path import expanduser from datetime import datetime class ExcelToHDF5Converter: def __init__(self, root): self.root = root self.root.title("Excel转HDF5") self.folder_path = "" self.setup_ui() def setup_ui(self): self.center_window(350, 180) # set window root.configure(background='#f0f0f0') self.convert_button = tk.Button(self.root, text="Select a Excel File", width=30, command=self.start_convert, padx=10, pady=5) self.convert_button.pack(pady=20) self.open_path_button = tk.Button(self.root, text="open folder", width=30, command=self.open_path, padx=10, pady=5) self.open_path_button.pack(pady=20) self.open_path_button.config(state=tk.DISABLED) def center_window(self, width, height): screen_width = self.root.winfo_screenwidth() screen_height = self.root.winfo_screenheight() x = (screen_width / 2) - (width / 2) y = (screen_height / 3) - (height / 2) self.root.geometry('%dx%d+%d+%d' % (width, height, x, y)) def start_convert(self): file_path = filedialog.askopenfilename(filetypes=[("Excel文件", "*.xlsx *.xls")]) if not file_path: # messagebox.showinfo("message", "select excel file") return self.convert_button.config(text="Converting, please wait...", state=tk.DISABLED) threading.Thread(target=self.convert_to_hdf5, args=(file_path,)).start() def convert_to_hdf5(self, file_path): error_message = '' # error_message downloads_path = os.path.join(expanduser("~"), 'Downloads') current_time = datetime.now().strftime("%Y%m%d%H%M%S") original_path = Path(file_path) new_filename = f"{original_path.stem}-{current_time}" hdf5_filename = Path(new_filename).with_suffix('.h5') hdf5_path = Path(downloads_path) / hdf5_filename try: xls = pd.ExcelFile(file_path) with pd.HDFStore(hdf5_path, 'w') as hdf: for sheet_name in xls.sheet_names: df = pd.read_excel(xls, sheet_name=sheet_name, dtype=str) df.fillna("", inplace=True) # replace NaN to "" df = df.astype(str) safe_sheet_name = sheet_name.replace(' ', '_').replace('-', '_') if df.shape[0] < 3: # if data line Подробнее здесь: [url]https://stackoverflow.com/questions/78241851/error-sequence-item-2-expected-str-instance-nonetype-found-while-operating-h[/url]