如何使用条形码扫描仪将Python代码与Excel预读表链接起来,将学生数据输入到Excel表中?

问题描述 投票:0回答:1

我需要创建将在 pycharm 中使用的代码来链接 Excel 电子表格,并且我需要使用条形码扫描仪将学生数据输入到该 Excel 电子表格中以扫描学生条形码。另外,我需要在 Excel 电子表格本身中生成条形码,我已经完成了。但我想知道我是否能够使用我在 Excel 中生成的条形码并扫描这些条形码以在 Excel 工作表本身中输入学生数据。条形码仅包含学生的姓氏和名字。但 Excel 工作表必须具有以下标题:学生条形码 ID、学生姓名、学生上个月参加过吗?对于代码的这一部分,必须说明学生上个月是否参加“是或否。取决于学生是否参加。代码的另一部分必须说“总出勤天数”,电子表格必须总结总出勤天数代码的最后部分必须注明当月每一天的第一天、第二天、第三天、第四天等。与该月的每一天相匹配,例如 4 月 1 日星期一、2 日星期二等,并检查学生是否在一周或一个月中的每一天出席,在该特定学生上学的每一天都必须有数字 1参加的日期是星期一、星期二、星期三等

import datetime
import openpyxl

# Function to update attendance in Excel sheet
def update_attendance(student_name, attendance_status):
  today = datetime.datetime.now()
  month = today.strftime("%B")
  year = today.year
  date = today.strftime("%B %d")

  # Open the Excel file
  wb = openpyxl.load_workbook('attendance.xlsx')
  sheet = wb.active

  # Find the column for today's date
  col = 0
  for i in range(1, sheet.max_column + 1):
     if sheet.cell(row=1, column=i).value == date:
      col = i
      break

  # If today's date column not found, add it
  if col == 0:
    col = sheet.max_column + 1
    sheet.cell(row=1, column=col).value = date

  # Find the row for the student
  row = 0
  for i in range(2, sheet.max_row + 1):
    if sheet.cell(row=i, column=1).value == student_name:
      row = i
      break

  # If student not found, add a new row
  if row == 0:
   row = sheet.max_row + 1
   sheet.cell(row=row, column=1).value = student_name

  # Update attendance
  sheet.cell(row=row, column=col).value = attendance_status

  # Save the workbook
  wb.save('attendance.xlsx')

  # Example usage
  student_barcode = input("Scan student barcode: ")
  # Assuming the barcode contains student name and attendance status (e.g., "John Doe 1")
  student_name, attendance_status = student_barcode.split()

  update_attendance(student_name, attendance_status)

  # This is the code that I came up with but I haven't tried yet because I don't know if it is right        or not. I want to make sure that the code is the right one before I can test it out. 
datetime keyboard openpyxl
1个回答
0
投票

以下是如何使用 Python 和 Openpyxl 构建您所描述的考勤系统:

  1. 依赖关系:

    import datetime
    import openpyxl
    from openpyxl.styles import PatternFill
    
  2. 初始设置:

    Excel 电子表格:

    确保您的电子表格包含以下列:

    “学生条形码 ID”(填充有条形码)

    “学生姓名”

    “该学生上个月参加过吗?” (是/否)

    “总出勤天数”(每日出勤总和的公式)

    第 1 天,第 2 天,...(每月每一天的列)

    条码扫描器:

    设置条形码扫描仪以读取电子表格中的条形码。扫描仪应以字符串形式输出学生的姓氏和名字(如条形码中编码的那样)。

  3. 代码结构:

    def update_attendance(student_name):
       # ... (code to update attendance)
    
    def get_current_month_dates():
       # ... (code to get a list of dates for the current month)
    
    # ... (other functions as needed)
    
    # Main execution
    if __name__ == "__main__":
       student_barcode = input("Scan student barcode: ")
       student_name = student_barcode  # Assuming barcode only holds student name
       update_attendance(student_name)
    
  4. 实现update_attendance函数:

     def update_attendance(student_name):
      today = datetime.date.today()
      month_start = today.replace(day=1)
      month_end = month_start + datetime.timedelta(days=31)
      month_dates = get_current_month_dates()
    
      wb = openpyxl.load_workbook('attendance.xlsx')
      sheet = wb.active
    
      # Find student row
      student_row = None
      for row in range(2, sheet.max_row + 1):
         if sheet.cell(row, 2).value == student_name:  # Check "Student Name" column
           student_row = row
           break
    
         if not student_row:
            print(f"Student {student_name} not found in the spreadsheet.")
            return
    
         # Update attendance for today
         today_col = month_dates.index(today) + 5  # Assuming Day 1 starts at column 5
          sheet.cell(student_row, today_col).value = 1
          sheet.cell(student_row, today_col).fill = PatternFill("solid", fgColor="C0FFC0")  # Highlight the cell (optional)
    
           # Update total attendance for the month
           total_attendance_col = 4  # Assuming "Total attendance days" is in column 4
           total_attendance = sum(cell.value for cell in          sheet.iter_rows(min_row=student_row, max_row=student_row, min_col=5, max_col=today_col) if cell.value)
           sheet.cell(student_row, total_attendance_col).value = total_attendance
    
           # Update last month attendance (Yes/No) based on total attendance
           last_month_attendance_col = 3  # Assuming column for last month attendance
           sheet.cell(student_row, last_month_attendance_col).value = "Yes" if total_attendance > 0 else "No"
    
           wb.save('attendance.xlsx')
    
  5. 实现 get_current_month_dates 函数:

    def get_current_month_dates():
       today = datetime.date.today()
       month_start = today.replace(day=1)
       month_end = month_start + datetime.timedelta(days=31)
       month_dates = []
       while month_start <= month_end:
       if month_start.month == today.month:
         month_dates.append(month_start)
         month_start += datetime.timedelta(days=1)
         return month_dates
    
© www.soinside.com 2019 - 2024. All rights reserved.