import openpyxlimport keyboarddef on_key_pressevent s = streventname value = get_values if value # Load the workbook and the desired sheet workbook = openpyxlload_workbookDSTRES
修改后的代码如下:
import openpyxl
import keyboard
def on_key_press(event):
s = str(event.name)
value = get_value(s)
if value:
# Load the workbook and the desired sheet
workbook = openpyxl.load_workbook('D:\STRESSTOOL\KB\DQA_Keyboard_V1.0.0.0\FNC020 Keyboard Function Test -Founction key v3100 0325_R.XLSX')
sheet = workbook['Scan code Report (US)']
# Write the values to the respective cells
sheet['D11'].value = value[0] if len(value) >= 1 else ""
sheet['D12'].value = value[1] if len(value) >= 2 else ""
sheet['D30'].value = value[2] if len(value) >= 3 else ""
# Save the workbook
workbook.save('D:\STRESSTOOL\KB\DQA_Keyboard_V1.0.0.0\FNC020 Keyboard Function Test -Founction key v3100 0325_R.XLSX')
# Print the values
print("Values saved in the Excel file:", value)
def get_value(s):
dictionary = {'esc': '01', 'f1': '3b', 'f2': '3c', 'f3': '3d', 'f4': '3e', 'f5': '3f', 'f6': '40', 'f7': '41', 'f8': '42', 'f9': '43', 'f10': '44', 'f11': '57', 'f12': '58', 'print screen': 'e037', 'delete': 'e053', '`': '29', '1': '02', '2': '03', '3': '04', '4': '05', '5': '06', '6': '07', '7': '08', '8': '09', '9': '0a', '0': '0b', '-': '0c', '=': '0d', 'backspace': '0e', 'tab': '0f', 'q': '10', 'w': '11', 'e': '12', 'r': '13', 't': '14', 'y': '15', 'u': '16', 'i': '17', 'o': '18', 'p': '19', '[': '1a', ']': '1b', '\\ \\': '2b', 'caps lock': '3a', 'A': '1e', 'S': '1f', 'D': '20', 'F': '21', 'G': '22', 'H': '23', 'J': '24', 'K': '25', 'L': '26', ';': '27', '\'': '28', 'enter': '1c', 'shift': '2a', 'z': '2c', 'x': '2d', 'c': '2e', 'v': '2f', 'b': '30', 'n': '31', 'm': '32', ',': '33', '.': '34', '/': '35', 'right shift': 'e036', 'ctrl': '1d', 'left windows': '5b', 'alt': '38', 'space': '39', 'right ctrl': '1d', 'left': '4b', 'up': '48', 'down': '50', 'right': '4d', '': ''}
if s in dictionary:
return tuple(dictionary[s])
else:
return () # Return an empty tuple when not found
keyboard.on_press(on_key_press)
keyboard.wait('esc')
keyboard.unhook_all()
这段代码会在按下键盘上的任意键时执行on_key_press函数。根据按下的键名,该函数会从字典中获取相应的值,并将其填入Excel文件的相应单元格中。具体地,第一个值填入表单名为"Scan code Report (US)"的D11格内,第二个值填入D12格内,第三个值填入D30格内。最后,Excel文件会被保存
原文地址: http://www.cveoy.top/t/topic/iYhh 著作权归作者所有。请勿转载和采集!