客户管理系统
废话少说直接上代码import os.path
import openpyxl
from openpyxl.styles import Alignment
# 创建excel表格
def create():
# 创建excel表格
wb = openpyxl.Workbook()
ws = wb.active
# 设置格式
al = Alignment(
horizontal='left',# 水平方向:center, left, right
vertical='center'# 垂直方向: center, top, bottom
)
for cell in ws['A']:
cell.number_format = '@'
ws['A1'].alignment = al
ws.column_dimensions['A'].width = 20
ws['B1'].alignment = al
ws['C1'].alignment = al
ws['D1'].alignment = al
ws['E1'].alignment = al
# 添加信息
ws['A1'] = '身份证件号'
ws['B1'] = '姓名'
ws['C1'] = '房间号'
ws['D1'] = '房费'
ws['E1'] = '押金'
# 保存
wb.save('信息表.xlsx')
# 查询住户信息
def Inquire():
"""
:rtype: object
"""
wb = openpyxl.load_workbook('信息表.xlsx')
my_sheet = wb.worksheets
# 循环每一行除了表头
for row in my_sheet:
ID = row# 身份证所在的列
product_value = ID.value
product_value1 = str(product_value)
while True:
Ch = input("请输入身份证件号:")
if product_value1 == Ch:
if product_value != '身份证件号':
price_column = row# 身份证所在的列
ID_card = price_column.value
price_column = row# 姓名所在的列
name = price_column.value
price_column = row# 房间号所在的列
room_number = price_column.value
price_column = row# 房费所在的列
Room_rate = price_column.value
price_column = row# 押金所在的列
deposit = price_column.value
print(f"身份证件号:{ID_card} 姓名:{name} 房间号:{room_number} 房费:{Room_rate} 押金:{deposit}")
break
else:
print("没有该用户")
# 修改住户信息
def modify():
while True:
wb = openpyxl.load_workbook('信息表.xlsx')
ws = wb.active
my_sheet = wb.worksheets
# 循环每一行除了表头
Ch = Inquire()
Ch1 = input("再次确认修改住户信息(是/否):")
if Ch1 == "是":
# 循环第一个列表
for row in my_sheet:
ID = row
product_value = ID.value
product_value1 = str(product_value)
# 判断与用户输入进行判断
if product_value1 == Ch:
ID1 = str(ID)
data = []
data1 = ['A', 'B', 'C', 'D', 'E']
information = ["身份证件号:", "姓名:", "房间号:", "房费:", "押金:"]
j = 0
for i in ID1:
data.append(i)
del data[:15]
data = data
# 判断列表有是一位还是两位
if len(data) == 2:
for i in data1:
name = i + data + data
ws = input(information)
j += 1
else:
for i in data1:
name = i + data
ws = input(information)
j += 1
for cell in ws['A']:
cell.number_format = '@'
wb.save('信息表.xlsx')
break
elif Ch1 == "否":
break
# 添加住户
def Add_to():
# 打开excel表格
wb = openpyxl.load_workbook('信息表.xlsx')
# 获取最后一行并加一行
sheet = wb['Sheet']
max_row = sheet.max_row
line = max_row + 1
# 输入数据
num = input("身份证件号:")
name = input("姓名:")
num2 = input("房间号:")
num3 = input("房费:")
num4 = input("押金:")
# 导入到excel表格里
sheet.cell(line, 1).value = num
sheet.cell(line, 2).value = name
sheet.cell(line, 3).value = num2
sheet.cell(line, 4).value = num3
sheet.cell(line, 5).value = num4
# 保存
wb.save('信息表.xlsx')
# 删除住户
def delete():
Ch = Inquire()
Ch1 = input("再次删除住户信息(是/否):")
wb = openpyxl.load_workbook('信息表.xlsx')
ws = wb.active
my_sheet = wb.worksheets
while True:
if Ch1 == "是":
# 循环第一个列表
for row in my_sheet:
ID = row
product_value = ID.value
product_value1 = str(product_value)
# 判断与用户输入进行判断
if product_value1 == Ch:
# 因为本人能力有限只可以用获取到单元格的位置然后获取到数字然后进行删除
ID1 = str(ID)
data = []
for i in ID1:
data.append(i)
del data[:15]
data = data
# 判断列表有是单还是双
if len(data) == 2:
name = int(data + data)
ws.delete_rows(name)
else:
name = int(data)
ws.delete_rows(name)
# 保存
wb.save("信息表.xlsx")
print("已删除")
break
elif Ch1 == "否":
break
else:
print("没有该用户的信息")
# 选择
def select():
while True:
print("1:查询住户信息2:修改住户信息 3:添加住户 4:删除住户 5:退出")
Ch = input("请选择:")
if Ch == "1":
Inquire()
continue
elif Ch == "2":
modify()
continue
elif Ch == "3":
Add_to()
continue
elif Ch == "4":
delete()
continue
elif Ch == "5":
break
else:
print("输入错误")
# 判断是否有信息表
if not os.path.exists('信息表.xlsx'):
create()
print("创建成功")
Add_to()
else:
select()
写的有点菜,还在慢慢完成 为什么添加信息后查询和删除的时候显示无用户,表格里确实有了用户
页:
[1]