Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from openpyxl import Workbook
- from openpyxl.cell import get_column_letter
- wb = Workbook()
- ws = wb.active
- ws.title = "Пищевые добавки".decode('utf-8')
- buf=['','','']
- row=0
- column_widths=[0,0,0]
- infile=open('input.txt','r')
- for l in infile.readlines():
- #l=l.decode('cp1251')
- if len(l)==74: #parse line
- if '--' in l:
- lines=max([len(b.split('\n')) for b in buf])
- for i,b in enumerate(buf):
- b_len=max([len(bb) for bb in b.split('\n')])
- if column_widths[i]<b_len:
- column_widths[i]=b_len
- c=ws.cell(row=row,column=i)
- c.style.alignment.wrap_text = True
- c.value=b.decode('cp1251')
- ws.row_dimensions[row+1].height=20*lines
- row+=1
- buf=['','','']
- else:
- data=l.split('|')[1:4]
- if len(data[0])>2:
- buf[0]+=data[0].strip()+'\n'
- if len(data[1])>2:
- buf[1]+=data[1].strip()+'\n'
- if len(data[2])>2:
- if buf[2].endswith('-\n'): buf[2]=buf[2][:-2]+data[2].strip()+'\n'
- else: buf[2]+=data[2].strip()+'\n'
- #print column_widths
- for i, column_width in enumerate(column_widths):
- ws.column_dimensions[get_column_letter(i+1)].width = column_width
- wb.save('result.xlsx')
- print "%s lines written" % row
- print "Done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement