Advertisement
Guest User

Untitled

a guest
Mar 6th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from openpyxl import Workbook
  3. from openpyxl.cell import get_column_letter
  4. wb = Workbook()
  5. ws = wb.active
  6. ws.title = "Пищевые добавки".decode('utf-8')
  7. buf=['','','']
  8. row=0
  9. column_widths=[0,0,0]
  10. infile=open('input.txt','r')
  11. for l in infile.readlines():
  12.     #l=l.decode('cp1251')
  13.     if len(l)==74: #parse line
  14.         if '--' in l:
  15.             lines=max([len(b.split('\n')) for b in buf])
  16.             for i,b in enumerate(buf):
  17.                 b_len=max([len(bb) for bb in b.split('\n')])
  18.                 if column_widths[i]<b_len:
  19.                     column_widths[i]=b_len
  20.                 c=ws.cell(row=row,column=i)
  21.                 c.style.alignment.wrap_text = True
  22.                 c.value=b.decode('cp1251')
  23.             ws.row_dimensions[row+1].height=20*lines
  24.             row+=1
  25.             buf=['','','']
  26.         else:
  27.             data=l.split('|')[1:4]
  28.             if len(data[0])>2:
  29.                 buf[0]+=data[0].strip()+'\n'
  30.             if len(data[1])>2:
  31.                 buf[1]+=data[1].strip()+'\n'
  32.             if len(data[2])>2:
  33.                 if buf[2].endswith('-\n'): buf[2]=buf[2][:-2]+data[2].strip()+'\n'
  34.                 else: buf[2]+=data[2].strip()+'\n'
  35. #print column_widths
  36. for i, column_width in enumerate(column_widths):
  37.     ws.column_dimensions[get_column_letter(i+1)].width = column_width
  38. wb.save('result.xlsx')
  39. print "%s lines written" % row
  40. print "Done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement