问题
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),
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.
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
回答1:
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()
回答2:
I am not sure how it is done in openpyxl, but in xlwt it is .save(). Try running a print(help(wb)), should tell you all submethods on that object.
来源:https://stackoverflow.com/questions/54132135/how-to-save-the-data-entered-in-the-textbox-in-excel-using-openpyxl