如何使用openpyxl保存在excel中的文本框中输入的数据 [英] how to save the data entered in the textbox in excel using openpyxl
问题描述
我制作了一个程序,可以输入发票编号并搜索excel文件(请参阅我的上一个问题:如何提取特定行输入特定行的值的值),
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),
现在我想使用openpyxl将程序获取的数据保存到新的Excel文件中, 但我不知道该怎么解决, 我正在使用python 3.7.0.
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.
我的代码是
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()
我该怎么做以及如何将显示的数据保存在新的Excel文件中
how can i do this and how can i save the data displayed ,in a new excel file
推荐答案
您可以创建另一个工作簿并将结果写入活动工作表.然后将工作簿保存到文件.下面是示例代码:
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()
这篇关于如何使用openpyxl保存在excel中的文本框中输入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!