[英]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.