2021年5月5日

phthon3讀取健保卡上的資料

有需求記錄姓名和身份證字號,找了一下資料,可以用python3做出來

1.確定電腦和讀卡機是正常的(驅動程式....)

2. 參考資料:https://ithelp.ithome.com.tw/questions/10198968

3.安裝python3 

4.安裝pyscard(https://pypi.org/project/pyscard/)

pip install pyscard

或pip3 install pyscard

5.記錄檔為日期.txt

py程式碼

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import winsound

from smartcard.System import readers

# define the APDUs used in this script
SelectAPDU = [ 0x00, 0xA4, 0x04, 0x00, 0x10, 0xD1, 0x58, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00 ]

ReadProfileAPDU = [ 0x00, 0xca, 0x11, 0x00, 0x02, 0x00, 0x00 ]

# get all the available readers
r = readers()

reader = r[0]

connection = reader.createConnection()
connection.connect()

data, sw1, sw2 = connection.transmit(SelectAPDU)
print ("Select Applet: %02X %02X" % (sw1, sw2))

data, sw1, sw2 = connection.transmit(ReadProfileAPDU)
'''卡片內容
print ("Command: %02X %02X" % (sw1, sw2))
print  ('Card Number : %s' % ''.join(chr(i) for i in data[0:12]))
print  ('Name : %s' % ''.join(chr(i) for i in data[12:18])) # Big5
print  ('ID Number : %s' % ''.join(chr(i) for i in data[32:42]))
print  ('Birthday : %s' % ''.join(chr(i) for i in data[43:49]))
print  ('Sex : %s' % ''.join(chr(i) for i in data[49:50]))
print  ('Card Date : %s' % ''.join(chr(i) for i in data[51:57]))
'''
#身份證字號
uid = '%s' % ''.join(chr(i) for i in data[32:42])

#姓名
name = "{}".format(bytes(data[12:32]).decode("big5"))
#寫入檔案
fo = open("output.txt", "r+")
str = uid +  name + '\n'
# Write a line at the end of the file.
fo.seek(0, 2)
line = fo.write( str )
fo.close()
#發出嗶聲顥示已記錄
winsound.Beep(600,500)
print(uid +  name +'已記錄')

下載我的.py檔https://drive.google.com/file/d/1I8uGrEiec7TJTLSr5uejaFdMZwD7V3lU/view?usp=sharing

編譯完的.exe檔:https://drive.google.com/file/d/1sIiEVUg_70c-hg0Ux2488cg3Qmk2tjsg/view?usp=sharing


2 則留言:

高藥師 提到...

請問一下。print('Name : %s' % ''.join(chr(i) for i in data[12:18]))這裡是12:18

為何name = "{}".format(bytes(data[12:32]).decode("big5")),這裡卻是12:32?

謝謝!
另外健保卡只能讀取這樣的資料?

chen estea 提到...

網路上很多可參考的code,
https://www.google.com/search?channel=fs&q=%E5%81%A5%E4%BF%9D%E5%8D%A1+github