我制作了一个程序,将输入发票编号并搜索excel文件(请参阅我的上一个问题:How to extract a particular row value on inputting value of a particular row)
现在,我想使用openpyxl将程序获取的数据保存到新的Excel文件中, 但我不知道该怎么解决, 我正在使用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文件中
答案 0 :(得分:0)
我不确定在openpyxl中是如何完成的,但是在xlwt中它是.save()。尝试运行print(help(wb)),应该告诉您该对象的所有子方法。
答案 1 :(得分:0)
您可以创建另一个工作簿并将结果写入活动工作表。然后将工作簿保存到文件。下面是示例代码:
outwb = openpyxl.Workbook()
ws = outwb.active
ws.append([1, 2, 3, 4])
outwb.save('result.xlsx')
outwb.close()