Python calls Xlrd&xlsxwirter

Source: Internet
Author: User

  1. #coding =utf-8
  2. #/usr/bin/env python
  3. Import xlsxwriter,xlrd
  4. Import sys,os.path
  5. fname = ' zm6.xlsx '
  6. If not os.path.isfile (fname):
  7. print u ' file path does not exist '
  8. sys.exit ()
  9. data = Xlrd.open_workbook (fname) # open fname file
  10. Data.sheet_names () # Get the name of all sheet in the XLS file
  11. Table = Data.sheet_by_index (0) # get an xls file by index No. 0 sheet
  12. nrows = table.nrows # Gets the row number of the table sheet
  13. Ncols = Table.ncols # Gets the total number of columns in the table worksheet
  14. Workbook = Xlsxwriter. Workbook (' zm6.xlsx ') #创建一个excel文件
  15. Worksheet = Workbook.add_worksheet () #创建一个工作表对象
  16. Worksheet.set_column (0,ncols,22) #设定列的宽度为22像素
  17. #border: border, align: alignment, bg_color: background color, font_size: font size, bold: font Bold
  18. top = Workbook.add_format ({' border ': 1, ' align ': ' center ', ' bg_color ': ' cccccc ', ' font_size ': ' bold ':True})
  19. Green = Workbook.add_format ({' border ': 1, ' align ': ' center ', ' bg_color ': ' green ', ' font_size ': 12})
  20. Yellow = Workbook.add_format ({' border ': 1, ' Bg_color ': ' yellow ', ' font_size ': 12})
  21. Red = Workbook.add_format ({' border ': 1, ' align ': ' center ', ' bg_color ': ' red ', ' font_size ': 12})
  22. blank = Workbook.add_format ({' Border ': 1})
  23. For i in xrange (nrows):
  24. Worksheet.set_row (i,22) #设定第i行单元格属性, height 22 pixels, row index starting from 0
  25. for J in xrange (ncols):
  26. Cell_value = Table.cell_value (i,j,) #获取第i行中第j列的值
  27. if i = = 0:
  28. Format = Top
  29. elif i = = 3 or i = = 6:
  30. format = Blank
  31. else:
  32. if J = = 0 or J = = 2:
  33. Format = Yellow
  34. elif J = = 1:
  35. format = Red
  36. elif J = = 3:
  37. Format = Green
  38. Green.set_num_format (' yyyy-mm-dd ') #设置时间格式
  39. Worksheet. Write (i,j,cell_value,format) #把获取到的值写入文件对应的行列
  40. Format.set_align (' vcenter ') #设置单元格垂直对齐
  41. Workbook. Close ()

Python calls Xlrd&xlsxwirter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.