Created
November 19, 2024 05:48
Скрипт на Python 3 для печати чеков-коррекции из CSV файла через кассу Штрих-М
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import win32com.client as win32 | |
import csv | |
import logging | |
import sys | |
import time | |
logging.basicConfig( | |
filename='myapp.log', | |
format='%(asctime)s - %(levelname)s - Number: %(message)s', | |
level=logging.DEBUG) | |
# Настройки подключения | |
settings = { | |
'password': '30', | |
'admin_password': '30', | |
} | |
connection_types = { | |
0: 'Локально', | |
1: 'Сервер ККМ (TCP)', | |
2: 'Сервер ККМ (DCOM)', | |
3: 'ESCAPE', | |
5: 'Эмулятор', | |
6: 'Подключение через ТСР-сокет', | |
} | |
def process_csv(file_path): | |
try: | |
with open(file_path, 'r', newline='', encoding='utf-8') as csvfile: | |
reader = csv.DictReader(csvfile, delimiter=';') | |
for row in reader: | |
# Извлекаем значения из строк | |
number = int(row['number']) | |
summ = float(row['summ'].replace(',', '.')) | |
type = bool(int(row['type'])) | |
device.Beep() | |
device.Password = 30 | |
device.CorrectionType = 0 | |
device.CalculationSign = 1 | |
device.Summ1 = summ | |
if type: | |
device.Summ2 = 0 | |
device.Summ3 = summ | |
else: | |
device.Summ2 = summ | |
device.Summ3 = 0 | |
device.Summ4 = 0 | |
device.Summ5 = 0 | |
device.Summ6 = 0 | |
device.Summ7 = 0 | |
device.Summ8 = 0 | |
device.Summ9 = summ | |
device.Summ10 = 0 | |
device.Summ11 = 0 | |
device.Summ12 = 0 | |
device.TaxType = 100000 | |
device.FNBuildCorrectionReceipt2() | |
device.WaitForPrinting() | |
device.FeedAfterCut = False | |
device.CutType = True # неполная отрезка | |
device.CutCheck() | |
# print(f"Number: {number}, Summ: {summ:.2f}, Type: {type}") | |
logging.info(number) | |
time.sleep(2) | |
except FileNotFoundError: | |
print(f"Ошибка: Файл '{file_path}' не найден.") | |
sys.exit(1) | |
except csv.Error as e: | |
print(f"Ошибка при чтении CSV-файла: {e}") | |
sys.exit(1) | |
# Проверка результата выполнения команды | |
def check_result(device, operation=""): | |
if device.ResultCode == 0: | |
print(f"{operation} успешно выполнено.") | |
return True | |
else: | |
print(f"Ошибка {operation}: [{device.ResultCode}] {device.ResultCodeDescription}") | |
return False | |
# Подключение к устройству | |
def connect_to_device(): | |
print("Подключение к кассе...") | |
device = win32.gencache.EnsureDispatch('Addin.DRvFR') | |
if not device: | |
raise Exception("Не удалось загрузить COM-объект Addin.DRvFR.") | |
device.SysAdminPassword = settings['admin_password'] | |
device.Password = settings['password'] | |
device.Connect() | |
if not check_result(device, "Подключение"): | |
raise Exception("Ошибка подключения.") | |
device.GetDeviceMetrics() | |
ct = connection_types.get(device.ConnectionType, "Неизвестно") | |
print(f"Подключено: {device.UDescription}, Тип подключения: {ct}") | |
return device | |
# Основной процесс | |
if __name__ == "__main__": | |
try: | |
device = connect_to_device() | |
file_path = 'list.csv' | |
process_csv(file_path) | |
except Exception as e: | |
print(f"Ошибка: {e}") | |
finally: | |
device.Disconnect() | |
print("Скрипт завершен.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment