如何使用openpyxl将所有数据导出到Excel文件?

de90aj5v  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(197)

我正尝试将用户输入的所有数据写入Excel文件,每个新条目都在最新条目下面输入。
单击提交后,它应该在GUI中执行此操作。
我设法让标题显示出来。
我正在使用openpyxl库,我在谷歌上搜索了一下,这是第一个出现的。
我的GUI供参考:

下面是我的代码部分:

filepath = "C:\\xxxx\\xxxx\\xxxx\\xxxx\\xxxx\\Collecting Data.xlsx"
            
if not os.path.exists(filepath):
    xlsx_open = openpyxl.Xlsx_openxlsx_open()
    sheet = xlsx_open.active
    table_headings = ["Card Type", "Staff Number", "Staff Name", "Staff Title", "Department", "Safety Function", "Safety Standards", "Brief Description of the Incident"]
    sheet.append(table_headings)
    xlsx_open.save(filepath)
    xlsx_open = openpyxl.load_xlsx_open(filepath)
    sheet = xlsx_open.active
    sheet.append([staff_name_entry, staff_id_dropdown, staff_title_entry, department_entry, 
                  card_type_dropdown, safety_function_dropdown, safety_standard_dropdown, manualy_text_entry])
    xlsx_open.save(filepath)

window.mainloop()

这是我得到的错误。
错误:submit_form sheet. append([卡片类型下拉列表,员工ID下拉列表,员工姓名条目,员工职务条目,部门条目,^^^^^^^^^^^^属性错误:"NoneType"对象没有属性"append"

w8f9ii69

w8f9ii691#

好吧,所以我最终弄清楚了自己通过以下变化:

  • 我创建了一个名为“entries”的新变量,其中包含输入列表,以便append函数将其识别为函数。
  • 我将“filepath”变量中的双“\”更改为单“/”,以解决“NoneType”错误。

我不确定是这两种解决方案的组合解决了这个问题,还是一种解决方案解决了这个问题,但不管怎样,它已经完成了任务,我将把这个问题留给对python更有经验的人来回答:)
P.S:我非常肯定代码可以看起来更好更简单,但再次强调,我是一个python新手...至少它能工作:D New and Working Code:

#Libraries that were used
import tkinter, os, openpyxl
from openpyxl import *
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

def submit_form():# submit form

    StaffIdDropdown = staff_id_dropdown.get()
    StaffNameEntry = staff_name_entry.get()
    StaffTitleEntry = staff_title_entry.get()
    DepartmentEntry = department_entry.get()
    CardTypeDropdown = card_type_dropdown.get()
    SafetyFunctionDropdown = safety_function_dropdown.get()
    SafetyStandardDropdown = safety_standard_dropdown.get()
    ManualTextEntry = manualy_text_entry.get("1.0",'end-1c')   
    entries = [CardTypeDropdown, StaffIdDropdown, StaffNameEntry, StaffTitleEntry, DepartmentEntry, SafetyFunctionDropdown, SafetyStandardDropdown, ManualTextEntry]
    
    print("The form has been submitted successfully with the following contents:\n", StaffNameEntry.upper(), " with the staff ID of ", StaffIdDropdown.upper(), 
        " and a title of ", StaffTitleEntry.upper(), " belonging to the ", DepartmentEntry.upper(), " Department.")
    print("-------------------------------------------------------------------------------------------------------")

    main_filepath = "C:/Users/JadJackHanna/OneDrive - Terminals Holding/Terminals Holding LLC/Application Python/ApplicationData.xlsx"

    if not os.path.exists(main_filepath):
        workbook = openpyxl.Workbook()
        sheet = workbook.active
        form_headings = ["Card Type", "Staff ID", "Staff Name", "Staff Title", "Department",
                            "Safety Function", "Safety Standard", "Brief Description of the Incident"]
        sheet.append(form_headings)
        sheet.append(entries)
        workbook.save(main_filepath)
    else:
        workbook = openpyxl.load_workbook(main_filepath)
        sheet = workbook.active
        sheet.append(entries)
        workbook.save(main_filepath)

    staff_id_dropdown.set('')
    staff_name_entry.delete(0, 'end')
    staff_title_entry.delete(0, 'end')
    department_entry.delete(0, 'end')
    card_type_dropdown.set('')
    safety_function_dropdown.set('')
    safety_standard_dropdown.set('')
    manualy_text_entry.delete('1.0', tkinter.END)
    
    # Confirmation Box
    messagebox.showinfo(title="Success!", message="The form as been submitted and saved!")

相关问题