简体   繁体   English

如何使用openpyxl保存在excel中的文本框中输入的数据

[英]how to save the data entered in the textbox in excel using openpyxl

i made a program that will input the invoice number and search the excel file(ref my previous question : How to extract a particular row value on inputting value of a particular row ), 我制作了一个程序,将输入发票编号并搜索excel文件(请参阅我的上一个问题: 如何在输入特定行的值时提取特定行值 ),

now i want to save the data fetched by the program into a new excel file using openpyxl, but i dont know what is the solution to this, i am using python 3.7.0. 现在我想使用openpyxl将程序获取的数据保存到一个新的excel文件中,但是我不知道该如何解决,我正在使用python 3.7.0。

my code is 我的代码是

from tkinter import *
import openpyxl

def update_text(info):
    book_info.delete(1.0, 'end')
    book_info.insert('end', info)

def find_book():
    inv_no = inv_field.get()
    if inv_no:
            wb = openpyxl.load_workbook('E:\Library Management\issue.xlsx')
            sheet = wb.active
            for row in sheet.rows:
                # assume invoice no is in column 1
                if row[0].value == inv_no:
                    update_text('\n'.join(str(cell.value) if cell.value else '' for cell in row))
                    return
            wb.close()
            update_text('Book not found')


a = Tk()
a.title('Return Book')
a.geometry('500x200')
heading = Label(a,text = 'Return Book')
heading.grid(row = 0,column = 1)
lab1 = Label(a,text = 'Enter Invoice Number:')
lab1.grid(row = 1, column = 0)
inv_field = Entry(a)
inv_field.grid(row = 1, column = 1)
inv_field.get()
find = Button(a,text = 'Find',width = 4,command =find_book)
find.grid(row = 2, column = 1)
book_info = Text(a, width=40, height=5)
book_info.grid(row = 3 ,column = 1)

 a.mainloop()

how can i do this and how can i save the data displayed ,in a new excel file 我该怎么做以及如何将显示的数据保存在新的Excel文件中

I am not sure how it is done in openpyxl, but in xlwt it is .save(). 我不确定在openpyxl中如何实现,但是在xlwt中它是.save()。 Try running a print(help(wb)), should tell you all submethods on that object. 尝试运行一个print(help(wb)),应该告诉您该对象的所有子方法。

You can create another workbook and write the result into the active sheet. 您可以创建另一个工作簿并将结果写入活动工作表。 Then save the workbook to file. 然后将工作簿保存到文件。 Below is an sample code: 下面是一个示例代码:

outwb = openpyxl.Workbook()
ws = outwb.active
ws.append([1, 2, 3, 4])
outwb.save('result.xlsx')
outwb.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用openpyxl将qtablewidget数据保存到excel文件中 - how to save qtablewidget data into excel file using openpyxl 如何使用 openpyxl 将字典数据保存到匹配的 excel 行但不同的列 - How do you save dictionary data to the matching excel row but different column using openpyxl 如何使用 Openpyxl Python 将转换后的文件保存到新的 excel 文件中? - How to save transformed file into new excel file using Openpyxl Python? 使用 Openpyxl excel 中的重复数据 - Duplicated data in excel using Openpyxl 如何使用 openpyxl 搜索 excel 中的数据? - How search data in excel with openpyxl? 如何使用 openpyxl / pandas 或任何 python 将从几个 excel 工作表中提取的字符串数据保存到新工作簿? - How do I save the string data I have extracted from several excel sheets to a new workbook using openpyxl / pandas or anything python? 如何使用openpyxl在普通模式下保存Excel工作簿? - How to save excel workbook in normal mode with openpyxl? 如何使用openpyxl在excel图表中仅显示一个数据标签? - How to show only one data label in excel chart using openpyxl? 如何使用 Python 使用 openpyxl 将数据添加到现有的 excel 文件 - How to add data to an existing excel file with openpyxl using Python 使用openpyxl写入数据后如何忽略excel中的“启用编辑” - How to ignore “Enable Editing” in excel after writing data using openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM