zoukankan      html  css  js  c++  java
  • python读取excel,使用了pywin32模块

    http://hi.baidu.com/qjdsw/item/84ee94274023a00877272cc5

    http://blog.csdn.net/wjwbin1986/article/details/6159154

    python操作excel需要使用的win32com模块,可以从http://sourceforge.net/projects/pywin32/files/处下载。

     1 #!/usr/bin/env python  
     2 # -*- coding: utf-8 -*-  
     3 import win32com.client
     4  
     5 #---------------------------------------------------------------------------
     6 class easyExcel:
     7     '''
     8     Some convenience methods for Excel documents accessed
     9     through COM.
    10     '''
    11     def __init__(self, filename=None):
    12         '''
    13         Create a new application
    14         if filename is None, create a new file
    15         else, open an exsiting one
    16         '''
    17         self.xlApp = win32com.client.Dispatch('Excel.Application')
    18         self.xlApp.Visible = False
    19         if filename:
    20             self.filename = filename  
    21             self.xlBook = self.xlApp.Workbooks.Open(filename)  
    22         else:  
    23             self.xlBook = self.xlApp.Workbooks.Add()  
    24             self.filename = ''    
    25     def visible(self,visible = True):
    26         '''
    27         if Visible is true, the applicaion is visible
    28         '''
    29         self.Visible = visible
    30     def save(self, newfilename=None):
    31         '''
    32         if filename is None, save the openning file
    33         else save as another file used the given name
    34         '''
    35         if newfilename:
    36             self.filename = newfilename  
    37             self.xlBook.SaveAs(newfilename)  
    38         else:  
    39             self.xlBook.Save()      
    40     def close(self):
    41         '''
    42         Close the application
    43         '''
    44         self.xlBook.Close(SaveChanges=0)  
    45         del self.xlApp  
    46     def getCell(self, sheet, row, col):  
    47         '''
    48         Get value of one cell
    49         '''
    50         sht = self.xlBook.Worksheets(sheet)  
    51         return sht.Cells(row, col).Value  
    52     def setCell(self, sheet, row, col, value):  
    53         '''
    54         Set value of one cell
    55         '''
    56         sht = self.xlBook.Worksheets(sheet)  
    57         sht.Cells(row, col).Value = value 
    58     def getRange(self,sheet,row1,col1,row2,col2):
    59         '''
    60         Return a 2d array (i.e. tuple of tuples)
    61         '''
    62         sht = self.xlBook.Worksheets(sheet)
    63         return sht.Range(sht.Cells(row1,col1),sht.Cells(row2,col2)).Value
    64     def setRange(self,sheet,leftCol,topRow,data):
    65         '''
    66         Insert a 2d array starting at given location.
    67         i.e. [['a','b','c'],['a','b','c'],['a','b','c']]
    68         Works out the size needed for itself
    69         '''
    70         bottomRow = topRow + len(data) - 1
    71         rightCol = leftCol + len(data[0]) - 1
    72         sht = self.xlBook.Worksheets(sheet)
    73         sht.Range(sht.Cells(topRow, leftCol), sht.Cells(bottomRow, rightCol)).Value = data   
    74         
    75         
    76         
    77 if __name__ == "__main__":
    78     excelProxy = easyExcel("D:\chart_demo.xls")
    79     content=excelProxy.getRange("sheet1",4,1,6,3) 
    80     print content
    81 #    excelProxy.setRange("sheet1",2,1, ['a','b','c']) 
    82 #    excelProxy.save()
    83     excelProxy.close()
  • 相关阅读:
    jmeter中设置线程数与设置集合点的区别
    在linux系统中如何通过shell脚本批量设置redis键值对
    服务器带宽上行与下行的区别
    性能测试之Jmeter插件安装
    sqlserver 启用邮箱服务
    sqlserver 自定义字符串分割函数.
    C# 重写思想
    CSS控制鼠标滑过时的效果
    js实现图片自动切换效果。
    SQL Server Management Studio 使用作业实现数据库备份
  • 原文地址:https://www.cnblogs.com/lvxiuquan/p/3079997.html
Copyright © 2011-2022 走看看