Origin of accounting procedure
I used to write a shortcut command for accounting in the quick cut command of iOS14, but due to the limitation of shortcut command, only the data can be recorded to the memo or numbers, the data migration is very poor. So I ended up writing a program in Python
Program is introduced
The module
This program uses fewer modules, the content is as follows, the missing modules please install yourself:
- Openpyxl: module used for reading and writing XLSX files.
- Datetime: The date module used to get the time when you booked your account.
- Tkinter: Graphical module, used to make visual operation interface.
- OS: provides built-in modules for operating files without installation.
Data is written to
When creating a new file, write the title in the first line and assign the time string from datetime to A; Assign the consumption type to B and the consumption amount to C. Append the values of a, b, and c to the workbook. Because the date field is too long, the date column is set to be 20 in advance.
Zb = openPyxl.workbook () xf = zb.active xf['A1'] = 'date' xf['B1'] = 'date' xf['C1'] = 'date' a = datetime.datetime.now() b = Entry1.get() c = Entry2.get() xf.append([a, b, C]) xf.column_dimensions['A']. Width = 20 zb.save(' personal account.xlsx ') 1234567891011Copy the code
GUI Graphical interface
Made a simple interface, including two input boxes and two buttons.
Geometry ('300x120+700+300') label1 = Label(JZR, text=' 中 国 地 质 :', font=('微软雅黑', 10), Grid (row=0, column=0) label2 = Label(JZR, column=0) label2 = Label(JZR, column=0) Fg ='green') label2.grid(row=1, column=0) Entry1 = Entry(JZR, font=('微软雅黑', 12), width=16) entry1. grid(row=0, column=0) Entry1 = Entry(JZR, font=('微软雅黑', 12), width=16) The column Entry2 = = 1) Entry (JZR, the font = (' Microsoft elegant black, 12), width = 16) Entry2, grid (row = 1, the column = 1) for the = Button (JZR, Grid (row=3, column=0, sticky=W) Button2 = Button(JZR, column=0, sticky=W) Button2 = Button(JZR, column=0, sticky=W) Button2 = Button(JZR, column=0, sticky=W) Button2 = Button(JZR, column=0, = 'exit' text, the font = (' Microsoft elegant black, 10), width = 8, the command = JZR. Quit Button2). The grid (row = 3, column = 1, sticky=E) jzr.mainloop() 123456789101112131415Copy the code
About the input null value
If there is no data in the input box, no data is recorded, and an if function is used.
If con1 == ": messagebox.showinfo(' prompt ', message=' please input the consumption type ') elif con2 ==" : Messagebox.showinfo (' prompt ', message=' Please input spending amount ') else: 12345Copy the code
New Workbook
After the program runs, if it is run for the first time, it will create a workbook named “personal ledger”, and then write data; If the workbook already exists, it reads and adds new data. The if structure is used here and nested in the if statement above.
Else: if os.path.exists(' personal ledger.xlsx'): XLSX 'zb = OpenPyxl.load_workbook (filepath) xf = zb.active a = datetime.datetime.now() b = entry1.get () c = Entry2.get() xf.append([a, b, C]) xf.column_dimensions['A'].width = 20 zb.save(' XLSX ') messagebox. Message =' consumption data logged ') jzr.quit() else: Zb = openPyxl.workbook () xf = zb.active xf['A1'] = 'date' xf['B1'] = 'date' xf['C1'] = 'date' a = datetime.datetime.now() b = Entry1.get() c = Entry2.get() xf.append([a, b, C]) xf.column_dimensions['A'].width = 20 zb.save(' XLSX ') messagebox. Message = 'consumption data record has been finished) JZR. Quit (123456789101112131415161718192021222324252627)Copy the code
The complete code
import openpyxl from openpyxl import Workbook import datetime from tkinter import messagebox from tkinter import * Import OS JZR = Tk() jzr.title(' 中 文 本') jzr.geometry('300x120+700+300') label1 = Label(JZR, text=' 中 文 类 型 :', font=(' Microsoft yhei ', Grid (row=0, column=0) label2 = Label(JZR, column=0) label2 = Label(JZR, column=0) Fg ='green') label2.grid(row=1, column=0) Entry1 = Entry(JZR, font=('微软雅黑', 12), width=16) entry1. grid(row=0, column=0) Entry1 = Entry(JZR, font=('微软雅黑', 12), width=16) Column =1) Entry2 = Entry(JZR, font=(' Microsoft yablack ', 12), width=16) entry2. grid(row=1, column=1) def JZCX (): con1 = Entry1.get() con1 = con1.strip() con2 = Entry2.get() con2 = con2.strip() if con1 == '': Elif con2 == ": messagebox.showinfo(' prompt ', message=' please input the amount of consumption ') else: If os.path.exists(' personal ledge.xlsx '): XLSX 'zb = OpenPyxl.load_workbook (filepath) xf = zb.active a = datetime.datetime.now() b = entry1.get () c = Entry2.get() xf.append([a, b, C]) xf.column_dimensions['A'].width = 20 zb.save(' XLSX ') messagebox. Message =' consumption data logged ') jzr.quit() else: Zb = openPyxl.workbook () xf = zb.active xf['A1'] = 'date' xf['B1'] = 'date' xf['C1'] = 'date' a = datetime.datetime.now() b = Entry1.get() c = Entry2.get() xf.append([a, b, C]) xf.column_dimensions['A'].width = 20 zb.save(' XLSX ') messagebox. JZR. Quit () Button1 = Button(JZR, text=' 0 ', font=(' 0 ', 10), width=8, The command = JZCX) for the grid (row = 3, column = 0, sticky = W) Button2 = Button (JZR, text = 'exit', the font = (' Microsoft elegant black, 10), width = 8, command=jzr.quit) Button2.grid(row=3, column=1, sticky=E) jzr.mainloop()Copy the code
Get the full code here