一個文件版的名片管理系統(Python3)

記錄日常學習,文件尚有不足之處需要改正

下面的這個網址是編譯可運行的exe文件

https://download.csdn.net/download/valecalida/12026603

#!/usr/bin/python
# -*- coding: UTF-8-*-
# --author:valecalida--
# 主要以列表的功能實現,歡迎大家測試,提出您寶貴的意見
import sys
import os
import time
import xlwt
import xlrd
from pathlib import Path
user = {'vale':'111'}
Card_infos = []


def menu():
    print("----------Business Card Management System----------")
    print("\t\t\t1.添加一個新的名片")
    print("\t\t\t2.刪除一個名片")
    print("\t\t\t3.修改一個名片的信息")
    print("\t\t\t4.查詢一個名片的信息")
    print("\t\t\t5.顯示所有的名片的信息")
    print("\t\t\t6.刪除備份的名片信息")
    print("\t\t\t7.退出系統")
    print("----------Business Card Management System----------")


def add_card():
    print("請輸入要添加的信息:")
    card_info = []
    name = input("\t\t請輸入姓名:")
    qq_id = input("\t\t請輸入QQ ID:")
    tel = input("\t\t請輸入Telephone number:")

    card_info.append(name)
    card_info.append(qq_id)
    card_info.append(tel)
    Card_infos.append(card_info)
    print("正在新建,請稍後......")
    print("Done\n")


def save_card():
    myfile = xlwt.Workbook()
    myfile_sheet = myfile.add_sheet('sheet1')
    title_info = [['姓名', 'QQ ID', 'Telephone number']]
    row = 0
    for card_info in Card_infos:
        col = 0
        if row == 0:
            for title in title_info:
                for t in title:
                    myfile_sheet.write(row, col, t)
                    col += 1
                continue
        else:
            for card in card_info:
                myfile_sheet.write(row, col, card)
                col += 1
        row += 1
    myfile.save('Cards_Info.xls')


def reload_data():
    global Card_infos
    Card_infos = []
    try:
        reload_file = xlrd.open_workbook("Cards_Info.xls")
        reload_file_sheet = reload_file.sheet_by_index(0)
        index_row = 0
        for line in range(reload_file_sheet.nrows):
            card = []
            if line == 0:
                index_row += 1
                continue
        else:
            Card_infos.append(reload_file_sheet.row_values(index_row))
            # print(Card_infos)
    except Exception:
        pass



def change_card():

    if Card_infos:
        change_card_input = input("請輸入您要修改人的名字:")
        for info in Card_infos:
            if change_card_input == info[0]:

                name = input("請輸入姓名:")
                qq_id = input("請輸入QQ ID:")
                tel = input("請輸入Tel number:")

                info[0] = name
                info[1] = qq_id
                info[2] = tel
                print("正在修改,請稍後......\nDone\n")
                break
            else:
                print("系統中沒有人叫:" + change_card_input)
                break
    else:
        print("系統裏沒有任何信息,無法進行修改\n")


def del_card():
    if Card_infos:
        clear_choice = input("Enter 'a' to clear all;Enter 'n' to clear by name:")
        if clear_choice == 'a':
            Card_infos.clear()
            clear_info()
        elif clear_choice == 'n':
            del_name = input("請輸入您要刪除人的名字:")
            for info in Card_infos:
                if del_name == info[0]:
                    Card_infos.remove(info)
                    print("正在刪除,請稍後....\nDone.\n")
                    break
                else:
                    print("系統裏沒有關於這個人的信息,請仔細覈對")
                    break
        else:
            print("請確定您的輸入沒有問題!\n")
    else:
        print("系統裏沒有任何信息,無法進行刪除操作")


def search_card():
    # print(Card_infos)
    name = []
    for lines in Card_infos:
        name.append(lines[0])
    # print(name)
    if len(Card_infos) > 0:
        search_choice = input("請輸入你要查找人的名字:")
        if search_choice in name:
            index = name.index(search_choice)
            # print(index)
            # print((Card_infos[index])[0])
            print("\tName:" + (Card_infos[index])[0] + "\n\tQQ:" + (Card_infos[index])[1] + "\n\tTel num:" +
                  (Card_infos[index])[2] + "\n")
        else:
            print("系統中沒有人叫:" + search_choice + "\n")
    else:
        print("系統裏沒有任何信息,無法進行查詢\n")


def display_all():
    # print(Card_infos,type(Card_infos))
    if Card_infos:
        print("系統中現在有 %d 個名片信息" % (len(Card_infos)))
        for infos in Card_infos:
            print("第 %s 個是:" % (Card_infos.index(infos) + 1))
            print("\tName:" + str(infos[0]) + "\n\tQQ:" + str(infos[1]) + "\n\tTel num:" + str(infos[2]) + "\n")
    else:
        print("系統裏沒有任何名片信息.\n")


def turn_off():
    print("正在退出...")
    sys.exit()


def guest():
    user_name = input("請輸入您的名字:")
    user_pass = input("請輸入您的密碼:")
    if user_name in user_name and user_pass == user.get(user_name):
        print("welcome to Business Card Management System:")
        detect_file()
        menu()
    else:
        print("請確認您的賬號密碼是否正確,請稍後再試。")
        exit()


def detect_file():
    path = os.getcwd()
    myfile = Path(path + '\\''Cards_Info.xls')
    # print(myfile)
    if myfile.is_file():
        print("\t\t檢測到有之前保存的名片信息,正在載入...")
        time.sleep(1)
        print("載入成功!")
    else:
        print("沒有存儲的信息,將直接進入系統!")
    return myfile.is_file()


def clear_info():
    filename = os.listdir()
    if 'Cards_Info.xls' in filename:
        try:
            retcode = os.system("del Cards_Info.xls")
            if retcode == 0:
                print("已清空!")
            else:
                print("清理過程中好像出現了一點問題,請手工覈對一下。")
        except Exception:
            pass
    else:
        print("系統沒有備份文件,無需進行刪除操作!")

def main():
    reload_data()
    guest()
    while True:
        try:
            flag = True
            while flag:
                operate_code = int(input("請輸入要進行的操作:"))
                if 0 < operate_code <= 7:
                    # return operate_code
                    flag = False
                else:
                    print("請輸入1-7中間的值")
        except ValueError:
            print("請確定您的輸入符合輸入標準\n")
        else:
            if operate_code == 1:
                add_card()
                save_card()
            elif operate_code == 2:
                del_card()
                save_card()
            elif operate_code == 3:
                change_card()
                save_card()
            elif operate_code == 4:
                search_card()
            elif operate_code == 5:
                display_all()
            elif operate_code == 6:
                print("清除過程中不會影響程序正在佔用的數據!.")
                clear_info()
            elif operate_code == 7:
                # print(Card_infos)
                turn_off()


if __name__ == "__main__":
    main()

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章