jmunsch

Python: Excel Read xls xlsx / Write xlsx

Jul 31st, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. import xlrd, xlsxwriter
  2.  
  3. # open files
  4. workbook_in = xlrd.open_workbook('C:/Users/UserBob/Downloads/12661.xlsx')
  5. workbook_out = xlsxwriter.Workbook('test.xlsx')
  6.  
  7. # create formats for output
  8. header_format = workbook_out.add_format()
  9. header_format.set_bold()
  10. header_format.set_bg_color('#808080') # gray
  11. url_format = workbook_out.add_format({
  12.     'font_color': 'blue',
  13.     'underline':  1
  14. })
  15. url_format.set_bg_color('#ffffff')
  16.  
  17. #copy sheets
  18. sheet1 = 'Detail'
  19. sheet2 = 'Summary'
  20. for sheet_index in range(workbook_in.nsheets):
  21.     #copy sheet
  22.     sheet_in = workbook_in.sheet_by_index(sheet_index)
  23.     sheet_name = sheet_in.name
  24.     sheet_out = workbook_out.add_worksheet(sheet_name)
  25.     sheet_out.set_column('A:G', 30) # set column spacing for sheet_out
  26.     num_rows = sheet_in.nrows
  27.     num_cells = sheet_in.ncols
  28.  
  29.     print(sheet_name)
  30.     if sheet1 in str(sheet_name):
  31.         # grab header names
  32.         headers = [item.lower() for item in sheet_in.row_values(0,0)]
  33.         jnumber_index = headers.index('item')
  34.         title_index = headers.index('item description')
  35.         # # grab uts numbers create urls
  36.         uts_nums = []
  37.         for jnumber in sheet_in.col_values(jnumber_index,0):
  38.             uts_nums.append(jnumber[1:6])
  39.         for i,uts in enumerate(uts_nums):
  40.             uts_nums[i] = "http://url.com/product/"+str(uts)+".jpg"
  41.         # iterate rows
  42.         for row_index in range(num_rows):
  43.             row = sheet_in.row_values(row_index)
  44.             #iterate cells
  45.             for cell_index, cell_value in enumerate(row):
  46.                 # if title index write urls
  47.                 if (cell_index is title_index) and (row_index is not 0):
  48.                     #write urls
  49.                     print('Writing URL')
  50.                     sheet_out.write_url(row_index, cell_index,uts_nums[row_index],url_format,str(row[cell_index]))
  51.                 elif row_index is 0:
  52.                     sheet_out.write(row_index,cell_index, str(cell_value),header_format)
  53.                 else:
  54.                     print('Writing.')
  55.                     sheet_out.write(row_index,cell_index, str(cell_value))
  56.     elif sheet2 in str(sheet_name):
  57.         #iterate rows
  58.         for row_index in range(num_rows):
  59.             row = sheet_in.row_values(row_index)
  60.             # iterate cells
  61.             for cell_index, cell_value in enumerate(row):
  62.                 if row_index is 1:
  63.                     sheet_out.write(row_index,cell_index, str(cell_value), header_format)
  64.                 elif ('total' in row) or ('Total' in row):
  65.                     sheet_out.write(row_index,cell_index, str(cell_value), header_format)
  66.                 else:
  67.                     sheet_out.write(row_index,cell_index, str(cell_value))
  68.        
  69. # check
  70. import pdb;pdb.set_trace()
  71. # write test with:
  72. # workbook_out.close()
Advertisement
Add Comment
Please, Sign In to add comment