Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2225809
  • 博文数量: 287
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2130
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-31 14:30
个人简介

自己慢慢积累。

文章分类

全部博文(287)

分类: Python/Ruby

2016-05-31 14:43:08

http://blog.sina.com.cn/s/blog_3fcd4ff90100n2mb.html

点击(此处)折叠或打开

  1. #!usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. #Xlsverter.py changes input excel files to new excel files with different content.
  5. Input files like Pathway_in.xls and output files like Pathway_out.xls.
  6. Run the program in an WinXP OS with python2.6 and pywin32 for python2.6 installed.
  7. """
  8. import os
  9. import sys
  10. import string
  11. import operator
  12. import win32com.client
  13. class easyExcel:
  14.     def __init__(self, filename=None):
  15.         self.xlApp = win32com.client.Dispatch('Excel.Application')
  16.         if filename:
  17.             self.filename = filename
  18.             self.xlBook = self.xlApp.Workbooks.Open(filename)
  19.         else:
  20.             self.xlBook = self.xlApp.Workbooks.Add()
  21.             self.filename = ''
  22.    
  23.     def save(self, newfilename=None):
  24.         if newfilename:
  25.             self.filename = newfilename
  26.             self.xlBook.SaveAs(newfilename)
  27.         else:
  28.             self.xlBook.Save()
  29.     def close(self):
  30.         self.xlBook.Close(SaveChanges=0)
  31.         del self.xlApp
  32.        
  33.     def getCell(self, sheet, row, col):
  34.         "Get value of one cell"
  35.         sht = self.xlBook.Worksheets(sheet)
  36.         return sht.Cells(row, col).Value
  37.     def setCell(self, sheet, row, col, value):
  38.         "set value of one cell"
  39.         sht = self.xlBook.Worksheets(sheet)
  40.         sht.Cells(row, col).Value = value
  41.     def getRange(self, sheet, row1, col1, row2, col2):
  42.         "return a 2d array (i.e. tuple of tuples)"
  43.         sht = self.xlBook.Worksheets(sheet)
  44.         return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value
  45.    
  46.     def setRange(self, sheet, leftCol, topRow, data):
  47.         """insert a 2d array starting at given location.
  48.         Works out the size needed for itself"""
  49.         bottomRow = topRow + len(data) - 1
  50.         rightCol = leftCol + len(data[0]) - 1
  51.         sht = self.xlBook.Worksheets(sheet)
  52.         #清空所当前sheet里的有单元格
  53.         sht.Cells.Clear()
  54.         sht.Range(
  55.             sht.Cells(topRow, leftCol),
  56.             sht.Cells(bottomRow, rightCol)
  57.             ).Value = data
  58.         sht.Range(
  59.             sht.Cells(topRow, leftCol),
  60.             sht.Cells(topRow, rightCol)
  61.             ).Font.Bold = True
  62.        
  63.     def getContiguousRange(self, sheet, row, col):
  64.         """Tracks down and across from top left cell until it
  65.         encounters blank cells; returns the non-blank range.
  66.         Looks at first row and column; blanks at bottom or right
  67.         are OK and return None witin the array"""
  68.         sht = self.xlBook.Worksheets(sheet)
  69.         # find the bottom row
  70.         bottom = row
  71.         while sht.Cells(bottom + 1, col).Value not in [None, '']:
  72.             bottom = bottom + 1
  73.         # right column
  74.         right = col
  75.         while sht.Cells(row, right + 1).Value not in [None, '']:
  76.             right = right + 1
  77.         #设置第一行若干列为粗体
  78.         return sht.Range(sht.Cells(row, col), sht.Cells(bottom, right)).Value

  79. if __name__ == "__main__":
  80.     filename = ''
  81.     newfilename = ''
  82.     l = len(sys.argv)
  83.     if l==1:
  84.         filename = raw_input("Please enter your '.xls' file name: ")
  85.         newfilename = filename + '.out.xls'
  86.     elif l==2:
  87.         filename = sys.argv[1]
  88.         newfilename = filename + '.out.xls'
  89.     xls = easyExcel(filename)
  90.     val = xls.getContiguousRange('KEGG', 1, 1)
  91.     assert val[0] == ('PathwayName',
  92.                       'Total',
  93.                       'Pvalue',
  94.                       'Qvalue',
  95.                       'Gene',
  96.                       'InputSymbol'), 'Column structure is wrong!'
  97.     rows = [val[1]]
  98.     for row in val[2:]:
  99.         (pn, t, fmt_pv, qv, fmt_gene, fmt_ins) = row
  100.         pv = string.atof(fmt_pv)
  101.         gene = fmt_gene
  102.         ins = fmt_ins
  103.         for i in range(len(rows)):
  104.             r = rows[i]
  105.             if pn==r[0]:
  106.                 gene = r[4] + ';' + fmt_gene
  107.                 ins = r[5] + ';' + fmt_ins
  108.                 rows.remove(rows[i])
  109.                 break
  110.         rows.append((pn, t, pv, qv, gene, ins))
  111.     rows = sorted(rows, key=operator.itemgetter(2))
  112.     rows.insert(0, val[0])
  113.     newval = tuple(rows)
  114.     xls.setRange('KEGG', 1, 1, newval)
  115.     xls.save(newfilename)
  116.     xls.close()
  117.  
  118. 注意:如果没有安装Excel则self.xlApp = win32com.client.Dispatch('Excel.Application')
  119. 会报错,而没有给出文件的绝对路径self.xlBook = self.xlApp.Workbooks.Open(filename)有可能报错。

阅读(3225) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~