Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import openpyxl
  2.  
  3. wb = openpyxl.load_workbook('pbc_tb.xlsx', data_only=True)
  4. sheet = wb.get_sheet_by_name('tb')
  5. last_row = sheet.max_row
  6. import_tb = [['gl', 'description', 'net']]
  7.  
  8. for row in range(4, last_row + 1):
  9. row = str(row)
  10. gl_description = sheet['B' + row].value
  11. if gl_description == 'Total':
  12. continue
  13. gl, *description = gl_description.split(' - ')
  14. description = ' '.join(description)
  15. net = round(sheet['G'+row].value - sheet['H'+row].value, 0)
  16. import_tb.append([gl, description, net])
  17.  
  18. rounding = sum(row[2] for row in import_tb[1:])
  19.  
  20. if rounding != 1:
  21. import_tb.append(['9999', 'rounding', -rounding])
  22.  
  23. import_sheet = wb.create_sheet(title='tb_import', index=0)
  24.  
  25. for num, (gl, desc, net) in enumerate(import_tb):
  26. import_sheet['A' + str(num + 1)].value = gl
  27. import_sheet['B' + str(num + 1)].value = desc
  28. import_sheet['C' + str(num + 1)].value = net
  29.  
  30. wb.save('tb_import.xlsx')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement