码迷,mamicode.com
首页 > 编程语言 > 详细

Python相关

时间:2015-11-24 18:30:00      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

一、Pyinstaller

1、Pyinstaller2.1打包后的程序在中文目录中可能出现“未能加载python27.dll”错误,用Pyinstaller2.0打包就可以解决

二、Excel操作相关

1、可以用xlrd模块读取,xlwd模块只能创建新的文件,不能在原有的文件上修改,但是可以通过xlutils模块中copy()来操作。对于Excel2007及以上可能不兼容。

2、可以通过调用windows的com组件交互,示例如下:

 

import win32com.client

# 行号和列号从1开始
class EasyExcel:
    """A utility to make it easier to get at Excel.  Remembering
    to save the data is your problem, as is  error handling.
    Operates on one workbook at a time."""
    def __init__(self, filename=None):
        self.xlApp = win32com.client.DispatchEx(‘Excel.Application‘)
        if filename:
            self.filename = filename
            self.xlBook = self.xlApp.Workbooks.Open(filename)
        else:
            self.xlBook = self.xlApp.Workbooks.Add()
            self.filename = ‘‘
    def save(self, newfilename=None):
        if newfilename:
            self.filename = newfilename
            self.xlBook.SaveAs(newfilename)
        else:
            self.xlBook.Save()
    def close(self):
        self.xlBook.Close(SaveChanges=0)
        del self.xlApp
    def getCell(self, sheet, row, col):
        "Get value of one cell"
        sht = self.xlBook.Worksheets(sheet)
        return sht.Cells(row, col).Value
    def setCell(self, sheet, row, col, value):
        "set value of one cell"
        sht = self.xlBook.Worksheets(sheet)
        sht.Cells(row, col).Value = value
    def getRange(self, sheet, row1, col1, row2, col2):
        "return a 2d array (i.e. tuple of tuples)"
        sht = self.xlBook.Worksheets(sheet)
        return sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Value
    def addPicture(self, sheet, pictureName, Left, Top, Width, Height):
        "Insert a picture in sheet"
        sht = self.xlBook.Worksheets(sheet)
        sht.Shapes.AddPicture(pictureName, 1, 1, Left, Top, Width, Height)
    def cpSheet(self, before):
        "copy sheet"
        shts = self.xlBook.Worksheets
        shts(1).Copy(None,shts(1))

 

Python相关

标签:

原文地址:http://www.cnblogs.com/writingcode/p/python.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!