Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xlrd, xlsxwriter
- # open files
- workbook_in = xlrd.open_workbook('C:/Users/UserBob/Downloads/12661.xlsx')
- workbook_out = xlsxwriter.Workbook('test.xlsx')
- # create formats for output
- header_format = workbook_out.add_format()
- header_format.set_bold()
- header_format.set_bg_color('#808080') # gray
- url_format = workbook_out.add_format({
- 'font_color': 'blue',
- 'underline': 1
- })
- url_format.set_bg_color('#ffffff')
- #copy sheets
- sheet1 = 'Detail'
- sheet2 = 'Summary'
- for sheet_index in range(workbook_in.nsheets):
- #copy sheet
- sheet_in = workbook_in.sheet_by_index(sheet_index)
- sheet_name = sheet_in.name
- sheet_out = workbook_out.add_worksheet(sheet_name)
- sheet_out.set_column('A:G', 30) # set column spacing for sheet_out
- num_rows = sheet_in.nrows
- num_cells = sheet_in.ncols
- print(sheet_name)
- if sheet1 in str(sheet_name):
- # grab header names
- headers = [item.lower() for item in sheet_in.row_values(0,0)]
- jnumber_index = headers.index('item')
- title_index = headers.index('item description')
- # # grab uts numbers create urls
- uts_nums = []
- for jnumber in sheet_in.col_values(jnumber_index,0):
- uts_nums.append(jnumber[1:6])
- for i,uts in enumerate(uts_nums):
- uts_nums[i] = "http://url.com/product/"+str(uts)+".jpg"
- # iterate rows
- for row_index in range(num_rows):
- row = sheet_in.row_values(row_index)
- #iterate cells
- for cell_index, cell_value in enumerate(row):
- # if title index write urls
- if (cell_index is title_index) and (row_index is not 0):
- #write urls
- print('Writing URL')
- sheet_out.write_url(row_index, cell_index,uts_nums[row_index],url_format,str(row[cell_index]))
- elif row_index is 0:
- sheet_out.write(row_index,cell_index, str(cell_value),header_format)
- else:
- print('Writing.')
- sheet_out.write(row_index,cell_index, str(cell_value))
- elif sheet2 in str(sheet_name):
- #iterate rows
- for row_index in range(num_rows):
- row = sheet_in.row_values(row_index)
- # iterate cells
- for cell_index, cell_value in enumerate(row):
- if row_index is 1:
- sheet_out.write(row_index,cell_index, str(cell_value), header_format)
- elif ('total' in row) or ('Total' in row):
- sheet_out.write(row_index,cell_index, str(cell_value), header_format)
- else:
- sheet_out.write(row_index,cell_index, str(cell_value))
- # check
- import pdb;pdb.set_trace()
- # write test with:
- # workbook_out.close()
Advertisement
Add Comment
Please, Sign In to add comment