Advertisement
Toxotsist

Гайк 3

Apr 14th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. import re
  2. import time
  3.  
  4. import docx2pdf
  5. import docx.shared
  6. from docx import *
  7. import datetime
  8. import openpyxl
  9. import barcode
  10. from barcode.writer import ImageWriter
  11. import os
  12. from pyzbar import pyzbar
  13. import cv2
  14.  
  15.  
  16. class compInfo:
  17.  
  18. def __new__(cls, *args, **kwargs):
  19. return super().__new__(cls)
  20.  
  21. def __init__(self, receiver):
  22. self.sender = 'СУПЕРФИРМА'
  23. self.receiver = receiver
  24. self.reason = ''
  25. self.n_number = ''
  26. self.n_date = ''
  27. self.items = []
  28.  
  29. def __iter__(self):
  30. return self
  31.  
  32. def compile(self):
  33. time = datetime.date.today()
  34. self.n_date = str(time.day) + "." + str(time.month) + '.' + str(time.year)
  35.  
  36.  
  37. def clear():
  38. for file in os.listdir(os.getcwd()):
  39. if re.search(r'(накладная)|(AB)', file):
  40. os.remove(file)
  41.  
  42.  
  43. def parse():
  44. complist = []
  45. wb = openpyxl.load_workbook("data_isukk_8.xlsx")['27.03.2022']
  46. tmp = []
  47. for r in range(2, 71):
  48. tmp.append(wb.cell(r, 2).value)
  49. tmp = list(set(tmp))
  50. for comp in tmp:
  51. complist.append(compInfo(comp))
  52. for r in range(2, 71):
  53. for i, c in enumerate(complist):
  54. if wb.cell(r, 2).value == c.receiver:
  55. c.reason = wb.cell(r, 3).value
  56. c.n_number = i
  57. c.compile()
  58. tmp = [wb.cell(r, 4).value, wb.cell(r, 5).value, wb.cell(r, 6).value, wb.cell(r, 7).value,
  59. wb.cell(r, 6).value * wb.cell(r, 7).value]
  60. c.items.append(tmp)
  61. break
  62. return complist
  63.  
  64.  
  65. def brcode(title):
  66. code128 = barcode.get('code128', title, writer=ImageWriter())
  67. code128.default_writer_options['module_height'] = 3.0
  68. code128.default_writer_options['module_width'] = 0.05
  69. pic = code128.save(title)
  70. return title
  71.  
  72.  
  73. def generate_nacl():
  74. template = 'template.docx'
  75. data = parse()
  76. for c in data:
  77. template_doc = Document(template)
  78. style = template_doc.styles['Normal']
  79. font = style.font
  80. font.name = 'Times New Roman'
  81. font.size = docx.shared.Pt(14)
  82. file = 'AB_' + str(c.n_number) + '_' + c.n_date
  83. brcode(file)
  84. r = template_doc.paragraphs[0].add_run()
  85. r.add_picture(file + '.png', width=docx.shared.Cm(10))
  86. os.remove(file + '.png')
  87. for paragraph in template_doc.paragraphs:
  88. replace_text(paragraph, '{{N_NUMBER}}', str(c.n_number))
  89. replace_text(paragraph, '{{N_DATE}}', c.n_date)
  90. replace_text(paragraph, '{{SENDER}}', c.sender)
  91. replace_text(paragraph, '{{RECEIVER}}', c.receiver)
  92. replace_text(paragraph, '{{REASON}}', c.reason)
  93.  
  94. table = template_doc.tables[0]
  95. row_i = 1
  96. sum_quant = 0
  97. total = 0
  98. for item, e, p, q, s in c.items:
  99. table.add_row()
  100. table.rows[row_i].cells[0].text = str(row_i)
  101. table.rows[row_i].cells[1].text = str(item)
  102. table.rows[row_i].cells[2].text = str(e)
  103. table.rows[row_i].cells[3].text = str(p)
  104. table.rows[row_i].cells[4].text = str(q)
  105. table.rows[row_i].cells[5].text = str(s)
  106. row_i += 1
  107. sum_quant += int(q)
  108. total += int(s)
  109.  
  110. for paragraph in template_doc.paragraphs:
  111. replace_text(paragraph, '{{SUM_QUANT}}', str(sum_quant))
  112. replace_text(paragraph, '{{TOTAL}}', str(total))
  113.  
  114. template_doc.save('накладная_' + str(c.n_number) + '_' + str(c.receiver) + '.docx')
  115.  
  116.  
  117. def replace_text(paragraph, key, value):
  118. if key in paragraph.text:
  119. paragraph.text = paragraph.text.replace(key, value)
  120.  
  121.  
  122. def generate_lists():
  123. data = parse()
  124. template = 'template_list.docx'
  125. for c in data:
  126. template_doc = Document(template)
  127. style = template_doc.styles['Normal']
  128. font = style.font
  129. font.name = 'Times New Roman'
  130. font.size = docx.shared.Pt(14)
  131. file = 'AB_' + str(c.n_number) + '_' + c.n_date
  132. brcode(file)
  133. r = template_doc.paragraphs[0].add_run()
  134. r.add_picture(file + '.png', width=docx.shared.Cm(10))
  135. os.remove(file + '.png')
  136. for paragraph in template_doc.paragraphs:
  137. replace_text(paragraph, '{{N_NUMBER}}', str(c.n_number))
  138. replace_text(paragraph, '{{N_DATE}}', c.n_date)
  139. replace_text(paragraph, '{{SENDER}}', c.sender)
  140. replace_text(paragraph, '{{RECEIVER}}', c.receiver)
  141. replace_text(paragraph, '{{REASON}}', c.reason)
  142.  
  143. table = template_doc.tables[0]
  144. row_i = 1
  145. sum_quant = 0
  146. total = 0
  147. for item, e, p, q, s in c.items:
  148. table.add_row()
  149. table.rows[row_i].cells[0].text = str(row_i)
  150. table.rows[row_i].cells[1].text = str(item)
  151. table.rows[row_i].cells[2].text = str(e)
  152. table.rows[row_i].cells[3].text = str(q)
  153. row_i += 1
  154. sum_quant += int(q)
  155. total += int(s)
  156.  
  157. template_doc.save('AB_' + str(c.n_number) + '_' + str(c.receiver) + '.docx')
  158. docx2pdf.convert('AB_' + str(c.n_number) + '_' + str(c.receiver) + '.docx',
  159. 'AB_' + str(c.n_number) + '_' + str(c.receiver) + '.pdf')
  160. os.remove('AB_' + str(c.n_number) + '_' + str(c.receiver) + '.docx')
  161. return 0
  162.  
  163.  
  164. def draw_barcode(decoded, image):
  165. image = cv2.rectangle(image, (decoded.rect.left, decoded.rect.top),
  166. (decoded.rect.left + decoded.rect.width, decoded.rect.top +
  167. decoded.rect.height),
  168. color=(0, 255, 0), thickness=5)
  169. return image
  170.  
  171.  
  172. def decode(image):
  173. decoded_objects = pyzbar.decode(image)
  174. for obj in decoded_objects:
  175. image = draw_barcode(obj, image)
  176. # print("Type:", obj.type)
  177. # print("Data:", obj.data.decode("utf-8"))
  178. # print()
  179. for file in os.listdir(os.getcwd()):
  180. if re.search(normalize(str(obj.data.decode("utf-8"))), file):
  181. os.startfile(file)
  182. break
  183. time.sleep(15)
  184. return 0
  185.  
  186.  
  187. def normalize(s: str):
  188. a = s.split("_")
  189. return a[1]
  190.  
  191.  
  192. if __name__ == '__main__':
  193. # clear()
  194. # generate_nacl()
  195. # generate_lists()
  196. cap = cv2.VideoCapture(0)
  197. while True:
  198. _, frame = cap.read()
  199. decoded_objects = decode(frame)
  200. cv2.imshow("frame", frame)
  201. captured = pyzbar.decode(frame)
  202. if cv2.waitKey(1) == ord('q'):
  203. break
  204.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement