Guest User

Untitled

a guest
Mar 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. from openpyxl import load_workbook
  2.  
  3. wb = load_workbook("/Users/aloua/Downloads/xyl/test1/stats.xlsx", data_only=True)
  4. ws = wb.active
  5.  
  6. originalsheet = wb['original']
  7. copy = wb.copy_worksheet(originalsheet)
  8.  
  9. #originalsheet = wb.get_sheet_by_name('original')
  10.  
  11. def caculatesum(type, year, countryname):
  12. diarysum = 0
  13. for row in range(1,copy.max_row + 1):
  14. curtype = copy.cell(row, 3).value
  15. curyear = copy.cell(row, 4).value
  16. curcountryname = copy.cell(row, 1).value
  17. if curyear == year and curtype == type and curcountryname == countryname:
  18. diarysum += copy.cell(row, 5).value
  19. return diarysum
  20.  
  21.  
  22. countries = []
  23.  
  24. for country in originalsheet['A']:
  25. if country.value not in countries:
  26. countries.append(country.value)
  27. print countries
  28.  
  29. sheetlist = []
  30. for country in countries:
  31. newsheet = wb.create_sheet(country)
  32. sheetlist.append(newsheet)
  33.  
  34.  
  35. # print wb.sheetnames
  36.  
  37. types = []
  38.  
  39. for type in originalsheet['C']:
  40. if type.value not in types:
  41. types.append(type.value)
  42. print types
  43.  
  44. for sheet in sheetlist:
  45. print 'get information for {0}'.format(sheet.title)
  46. startRow = 1
  47. startCol = 1
  48. sheet.cell(startRow, startCol).value = sheet.title
  49. startCol += 1
  50. for i,type in enumerate(types):
  51. sheet.cell(startRow, i + startCol).value = type
  52. startRow += 1
  53. for year in range(2007, 2018):
  54. j = 1
  55. sheet.cell(startRow, j).value = year
  56. j += 1
  57. for i,type in enumerate(types):
  58. sheet.cell(startRow, i + j).value = caculatesum(type, year, sheet.title)
  59. startRow +=1
  60.  
  61. wb.save("/Users/aloua/Downloads/xyl/test1/test.xlsx")
Add Comment
Please, Sign In to add comment