Skip to content

Instantly share code, notes, and snippets.

@sarathlal-old
Created September 15, 2016 12:10
Show Gist options
  • Save sarathlal-old/188bc684e12c7ca305a96e32bc96a306 to your computer and use it in GitHub Desktop.
Save sarathlal-old/188bc684e12c7ca305a96e32bc96a306 to your computer and use it in GitHub Desktop.
import openpyxl
import csv
wb = openpyxl.load_workbook('test-file.xlsx')
#wb.get_sheet_names()
sheet = wb.get_sheet_by_name('Sheet1')
row_count = sheet.max_row
col_count = sheet.max_column
#print row_count
#print col_count
f = open('output_beautify.csv','w')
# Add Title row on first line
for x in range(1,col_count+1):
cell_title = str(sheet.cell(row=1, column=x).value)
f.write(cell_title)
if (x == col_count):
f.write("\n")
else:
f.write(",")
# end Title row
#initialize old_unique_identifier variable to check if it is a new product or product varient
rest_old_unique_identifier = ""
for i in range(2, row_count + 1):
#select a unique idetifier for the product
cur_unique_identifier = str(sheet.cell(row=i, column=4).value)
#print ("cur = " + cur_unique_identifier)
sep = ','
rest_cur_unique_identifier = cur_unique_identifier.split(sep, 1)[0]
#Remove color code from SKU with color code
k = rest_cur_unique_identifier.rfind("-")
sku = rest_cur_unique_identifier[:k]
#print(sku)
#Get the color
color = str(sheet.cell(row=i, column=12).value)
#Image folder
imgfldr = sku + "-" + color
#print(imgfldr)
'''
if(rest_cur_unique_identifier != rest_old_unique_identifier):
print("This is a new line")
varien = True
else:
varien = False
print("Not a new line")
'''
for m in range(1, col_count + 1):
#print(sheet.cell(row=1, column=m).value , sheet.cell(row=i, column=m).value)
string = str(sheet.cell(row=i, column=m).value)
if(m == 4):
string = sku
if (m == col_count):
f.write(string + ",")
f.write(imgfldr + "\n")
else:
f.write(string + ",")
'''
# Move cur_unique_identifier to old_unique_identifier
old_unique_identifier = cur_unique_identifier
#print("old = " + old_unique_identifier)
sep = ','
rest_old_unique_identifier = old_unique_identifier.split(sep, 1)[0]
print(rest_old_unique_identifier)
#old_unique_identifier_extraced =
'''
f.close()
'''
f = open('myfile','w')
f.write('hi there\n') # python will convert \n to os.linesep
f.close()
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment