Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. #Kyle Share
  2. import openpyxl
  3. import os
  4.  
  5. #MAC filepath
  6. #filepath = os.path.join('/Users', 'KyleShare', 'Programming', 'caravan', 'SAMS.XLSX' )
  7.  
  8. #WINDOWS filepath
  9. filepath = os.path.join('C:\\', 'Users', 'CaravanArms', 'Desktop', 'SAMS.XLSX' )
  10.  
  11. #Get workbook from filepath
  12. wb = openpyxl.load_workbook(filepath)
  13.  
  14. #Get all sheetnames
  15. sheet_names = wb.sheetnames
  16.  
  17. #Get first worksheet, since wb.active is set to 0 by default
  18. first_sheet = wb.active
  19.  
  20. #Create new workbook
  21. new_wb = openpyxl.Workbook()
  22. new_first_sheet = new_wb.active
  23.  
  24. def titles():
  25. titles = ["ACCOUNT(SBT CODE)", "PO#", "PO LINE", "CUSTOMER NAME", "ADDRESS 1(2ND LINE)", \
  26. "PHONE# (3RD LINE)", "ADDRESS 2", "CARRIER", "ITEM#", "ITEM DESCRIPTION", \
  27. "UNIT PRICE", "QTY", "LINE TOTAL", "TERMS"]
  28. title_index = 0
  29. for column_num in range(1, 15):
  30. new_first_sheet.cell(row = 1, column = column_num).value = titles[title_index]
  31. title_index += 1
  32.  
  33. #Functions copy data from reading, write to writing
  34. def account(writing, reading):
  35. new_first_sheet.cell(row = writing, column = 1).value = 'WMECOM'
  36.  
  37. def po_num(writing, reading):
  38. po = first_sheet.cell(row = reading, column = 1).value
  39. new_first_sheet.cell(row = writing, column = 2).value = po
  40.  
  41. def po_line(writing, reading):
  42. line = first_sheet.cell(row = reading, column = 13).value
  43. new_first_sheet.cell(row = writing, column = 3).value = line
  44.  
  45.  
  46. def customer_name(writing, reading):
  47. name = first_sheet.cell(row = reading, column = 63).value
  48. new_first_sheet.cell(row = writing, column = 4).value = name
  49.  
  50. #<Street address, Appt/Suite>
  51. #Appt/Suite may be in same cell as street address or 1 column right
  52. def address_1(writing, reading):
  53. street_address = first_sheet.cell(row = reading, column = 64).value
  54. apartment = first_sheet.cell(row = reading, column = 65).value
  55.  
  56. #if apartment exists on next column, add it to street address
  57. if apartment != None:
  58. street_address = "{} {}".format(street_address, apartment)
  59.  
  60. new_first_sheet.cell(row = writing, column = 5).value = street_address
  61.  
  62. def phone_num(writing, reading):
  63. phone = first_sheet.cell(row = reading, column = 78).value
  64. new_first_sheet.cell(row = writing, column = 6).value = phone
  65.  
  66. #<City, State Zip>
  67. def address_2(writing, reading):
  68. city = first_sheet.cell(row = reading, column = 66).value
  69. state = first_sheet.cell(row = reading, column = 67).value
  70. zip_code = first_sheet.cell(row = reading, column = 68).value
  71.  
  72. address2 = "{}, {} {}".format(city, state, zip_code)
  73. new_first_sheet.cell(row = writing, column = 7).value = address2
  74.  
  75. def carrier(writing, reading):
  76. new_first_sheet.cell(row = writing, column = 8).value = '3PT FDXG'
  77.  
  78. def item_num(writing, reading):
  79. item_num = first_sheet.cell(row = reading, column = 19).value
  80. new_first_sheet.cell(row = writing, column = 9).value = item_num
  81.  
  82. def item_desc(writing, reading):
  83. item_desc = first_sheet.cell(row = reading, column = 21).value
  84. new_first_sheet.cell(row = writing, column = 10).value = item_desc
  85.  
  86. def unit_price(writing, reading):
  87. unit_price = first_sheet.cell(row = reading, column = 16).value
  88. new_first_sheet.cell(row = writing, column = 11).value = unit_price
  89.  
  90. def quantity(writing, reading):
  91. quantity = first_sheet.cell(row = reading, column = 14).value
  92. new_first_sheet.cell(row = writing, column = 12).value = quantity
  93.  
  94.  
  95. def terms(writing, reading):
  96. new_first_sheet.cell(row = writing, column = 14).value = 'NET 60'
  97.  
  98. def main_info(writing, reading):
  99. account(writing, reading)
  100. po_num(writing, reading)
  101. customer_name(writing, reading)
  102. address_1(writing, reading)
  103. phone_num(writing, reading)
  104. address_2(writing, reading)
  105. carrier(writing, reading)
  106. terms(writing, reading)
  107.  
  108. def details(writing, reading):
  109. po_line(writing, reading)
  110. item_num(writing, reading)
  111. item_desc(writing, reading)
  112. unit_price(writing, reading)
  113. quantity(writing, reading)
  114.  
  115. def body():
  116. reading = 2
  117. writing = 2
  118. first_po = first_sheet.cell(row = reading, column = 1).value
  119. first_po_row = 2
  120.  
  121. for row in range(first_sheet.max_row):
  122. if first_po != None:
  123.  
  124. #Save main info since it is reused for multiple orders with same PO
  125. main_info(writing, reading)
  126. main_info_reading = reading
  127.  
  128. #Get details by reading from next row
  129. reading += 1
  130. details(writing, reading)
  131.  
  132. #After writing main info and details, write to and read from new row
  133. reading += 1
  134. writing += 1
  135.  
  136. #Save current PO to check against previous
  137. current_po = first_sheet.cell(row = reading, column = 1).value
  138.  
  139. #While PO stays same, create new lines to write to but pull main info from main line
  140. while current_po == first_po:
  141. #Copy new details for each line, but same main info
  142. main_info(writing, first_po_row)
  143. details(writing, reading)
  144.  
  145. reading += 1
  146. writing += 1
  147. current_po = first_sheet.cell(row = reading, column = 1).value
  148.  
  149. #When PO changes, save new PO and revert to normal pattern
  150. else:
  151. first_po = first_sheet.cell(row = reading, column = 1).value
  152. first_po_row = first_sheet.cell(row = reading, column = 1).row
  153.  
  154.  
  155. #Use quantity and Unit price to calculate line total
  156. def line_total():
  157. for row_num in range(2, new_first_sheet.max_row + 1):
  158. line_total = new_first_sheet.cell(row = row_num, column = 11).value * \
  159. new_first_sheet.cell(row = row_num, column = 12).value
  160. new_first_sheet.cell(row = row_num, column = 13).value = line_total
  161.  
  162. def main():
  163. titles()
  164. body()
  165. line_total()
  166.  
  167. main()
  168.  
  169. new_wb.save("SBT_SAMS.xlsx")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement