Python论坛  - 讨论区

标题:[python-chinese] python操作excel

2007年10月18日 星期四 14:52

??? ?? clfff.peter在gmail.com
星期四 十月 18 14:52:28 HKT 2007

Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
 ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071018/38497e2a/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月18日 星期四 15:00

大郎 iexper在gmail.com
星期四 十月 18 15:00:47 HKT 2007

myhat123 ¶ÔÕâ¸ö±È½ÏÁ˽â.
¾Ý˵ÊÇʹÓÃwinµÄcom×é¼þ.

On 10/18/07, ??? ?? <clfff.peter在gmail.com> wrote:
>
> Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
>  ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
> ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
ÿ¸öÈ˶¼ÓÐһƬÊôÓÚ×Ô¼ºµÄÉ­ÁÖ, ÃÔʧµÄÈËÃÔʧÁË, Ïà·êµÄ»¹»áÔÙÏà·ê...
Gtalk: iexper(at)gmail.com
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071018/ada3ff9c/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月18日 星期四 15:18

faitherQ faitherQ在163.com
星期四 十月 18 15:18:57 HKT 2007

¶ÔexcelµÄ¶Áд£¬ÓÐÁ½¸ö°ü£¬·Ö±ðÊÇxlrd ºÍ pyexcelerator

Äã¿ÉÒÔ·Ö±ðÏÂÔØ£¬¶øÇÒÀïÃæÓпØÖÆʾÀý¡£






faitherQ
2007-10-18



·¢¼þÈË£º ??? ??
·¢ËÍʱ¼ä£º 2007-10-18 14:52:47
ÊÕ¼þÈË£º python-chinese
³­ËÍ£º 
Ö÷Ì⣺ [python-chinese]python²Ù×÷excel

Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071018/05f7fdbb/attachment-0001.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月18日 星期四 16:10

??? ?? clfff.peter在gmail.com
星期四 十月 18 16:10:02 HKT 2007

ÎÒÏÖÔÚÔÚÓÃÕâ¸ö×ö£¬²»¹ýÓöµ½Ò»¸öÎÊÌ⣺
excelÖаüº¬¶àÓïÑÔ£¬µ±Óöµ½Ä³Ð©ÓïÑÔµÄʱºò£¬¾Í»áÓÐÒì³££¬Òì³£³öÏÖÔÚÎÒ½«cellÄÚÈÝת»»Îªstrʱ£¬´úÂëÈçÏÂ:
from win32com.client import Dispatch

xlApp = Dispatch("Excel.Application")
xlApp.Visible=1
doc=xlApp.Workbooks.Open('d:\\sample.xls')
sheet=doc.Sheets(2)
try:
    for i in range(1, 3, 1):
        s='raw(%d)' % i
        for j in range(1, 5, 1):
            cell=sheet.Cells(i,j)
            str1=str(cell)    # ¼ÙÉ赱ǰcellÖÐÓз¨ÎÄ£¬ÕâÀï»áÓÐÒì³£
            s+=str1.ljust(20)
        print s
finally:
    xlApp.Quit()
ÓÐʲô°ì·¨¿ÉÒÔ½«cellÖеÄÄÚÈÝÖ±½Óת»¯ÎªunicodeÂð£¿Ò²Ñ§ÕâÑù¾Í¿ÉÒÔÁË


ÔÚ07-10-18£¬´óÀÉ <iexper在gmail.com> дµÀ£º
>
> myhat123 ¶ÔÕâ¸ö±È½ÏÁ˽â.
> ¾Ý˵ÊÇʹÓÃwinµÄcom×é¼þ.
>
> On 10/18/07, ??? ?? <clfff.peter在gmail.com> wrote:
> >
> > Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
> >  ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
> > ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> ÿ¸öÈ˶¼ÓÐһƬÊôÓÚ×Ô¼ºµÄÉ­ÁÖ, ÃÔʧµÄÈËÃÔʧÁË, Ïà·êµÄ»¹»áÔÙÏà·ê...
> Gtalk: iexper(at)gmail.com
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071018/e15a1ff6/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月18日 星期四 16:19

Roger Zheng roger.zheng在gmail.com
星期四 十月 18 16:19:52 HKT 2007

我用过pyexcelerator,感觉不错。 里面的例子很多。想实现什么,按照例子做就ok。


On 10/18/07, ??? ?? <clfff.peter at gmail.com> wrote:
> 我现在在用这个做,不过遇到一个问题:
> excel中包含多语言,当遇到某些语言的时候,就会有异常,异常出现在我将cell内容转换为str时,代码如下:
> from win32com.client import Dispatch
>
>
> xlApp = Dispatch("Excel.Application")
> xlApp.Visible=1
> doc=xlApp.Workbooks.Open('d:\\sample.xls')
> sheet=doc.Sheets(2)
> try:
>     for i in range(1, 3, 1):
>         s='raw(%d)' % i
>         for j in range(1, 5, 1):
>             cell=sheet.Cells(i,j)
>             str1=str(cell)    # 假设当前cell中有法文,这里会有异常
>             s+=str1.ljust(20)
>         print s
> finally:
>     xlApp.Quit()
> 有什么办法可以将cell中的内容直接转化为unicode吗?也学这样就可以了
>
>
> 在07-10-18,大郎 <iexper at gmail.com> 写道:
> > myhat123 对这个比较了解.
> > 据说是使用win的com组件.
> >
> >
> > On 10/18/07, ??? ?? <clfff.peter at gmail.com > wrote:
> > >
> > > 想做个脚本处理excel文件,
> > >
> > > 网络上搜到的都比较零散,是个别几个api的调用示例,有没有比较完整的文档呀?
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> > > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> > >
> >
> >
> >
> > --
> > 每个人都有一片属于自己的森林, 迷失的人迷失了, 相逢的还会再相逢...
> > Gtalk: iexper(at)gmail.com
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>


-- 
Regards!

Roger Zheng

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月18日 星期四 19:05

haur hekun06在gmail.com
星期四 十月 18 19:05:02 HKT 2007

ºÃÏóÓиöxld¿â¸üºÃÓðÉ
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071018/704c5927/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月18日 星期四 19:37

hechu hcpython在gmail.com
星期四 十月 18 19:37:05 HKT 2007

一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20071018/6b2e1dee/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月18日 星期四 20:04

ttkk1024 ttkk1024在163.com
星期四 十月 18 20:04:44 HKT 2007

ÄãÏÈ°²×°xlrd
È»ºóÔÙ¿´¿´Õâ¸ö³ÌÐò

class readexcel(object):
    """ Simple OS Independent Class for Extracting Data from Excel Files 
        the using xlrd module found at http://www.lexicon.net/sjmachin/xlrd.htm
        
        Versions of Excel supported: 2004, 2002, XP, 2000, 97, 95, 5, 4, 3
        xlrd version tested: 0.5.2
        
        Data is extracted by creating a iterator object which can be used to 
        return data one row at a time. The default extraction method assumes 
        that the worksheet is in tabular format with the first nonblank row
        containing variable names and all subsequent rows containing values.
        This method returns a dictionary which uses the variables names as keys
        for each piece of data in the row.  Data can also be extracted with 
        each row represented by a list.
        
        Extracted data is represented fairly logically. By default dates are
        returned as strings in "yyyy/mm/dd" format or "yyyy/mm/dd hh:mm:ss",
        as appropriate.  However, dates can be return as a tuple containing
        (Year, Month, Day, Hour, Min, Second) which is appropriate for usage
        with mxDateTime or DateTime.  Numbers are returned as either INT or 
        FLOAT, whichever is needed to support the data.  Text, booleans, and
        error codes are also returned as appropriate representations.
        
        Quick Example:
        xl = readexcel('testdata.xls')
        sheetnames = xl.worksheets()
        for sheet in sheetnames:
            print sheet
            for row in xl.getiter(sheet):
                # Do Something here
        """ 
    def __init__(self, filename):
        """ Returns a readexcel object of the specified filename - this may
        take a little while because the file must be parsed into memory """
        import xlrd
        import os.path
        if not os.path.isfile(filename):
            raise NameError, "%s is not a valid filename" % filename
        self.__filename__ = filename
        self.__book__ = xlrd.open_workbook(filename)
        self.__sheets__ = {}
        self.__sheetnames__ = []
        for i in self.__book__.sheet_names():
            uniquevars = []
            firstrow = 0
            sheet = self.__book__.sheet_by_name(i)
            for row in range(sheet.nrows):
                types,values = sheet.row_types(row),sheet.row_values(row)
                nonblank = False
                for j in values:
                    if j != '':
                        nonblank=True
                        break
                if nonblank:
                    # Generate a listing of Unique Variable Names for Use as
                    # Dictionary Keys In Extraction. Duplicate Names will
                    # be replaced with "F#"
                    variables = self.__formatrow__(types,values,False)
                    unknown = 1
                    while variables:
                        var = variables.pop(0)
                        if var in uniquevars or var == '':
                            var = 'F' + str(unknown)
                            unknown += 1
                        uniquevars.append(str(var))
                    firstrow = row + 1
                    break
            self.__sheetnames__.append(i)
            self.__sheets__.setdefault(i,{}).__setitem__('rows',sheet.nrows)
            self.__sheets__.setdefault(i,{}).__setitem__('cols',sheet.ncols)
            self.__sheets__.setdefault(i,{}).__setitem__('firstrow',firstrow)
            self.__sheets__.setdefault(i,{}).__setitem__('variables',uniquevars[:])
    def getiter(self, sheetname, returnlist=False, returntupledate=False):
        """ Return an generator object which yields the lines of a worksheet;
        Default returns a dictionary, specifing returnlist=True causes lists
        to be returned.  Calling returntupledate=True causes dates to returned
        as tuples of (Year, Month, Day, Hour, Min, Second) instead of as a
        string """
        if sheetname not in self.__sheets__.keys():
            raise NameError, "%s is not present in %s" % (sheetname,\
                                                          self.__filename__)
        if returnlist:
            return __iterlist__(self, sheetname, returntupledate)
        else:
            return __iterdict__(self, sheetname, returntupledate)
    def worksheets(self):
        """ Returns a list of the Worksheets in the Excel File """
        return self.__sheetnames__
    def nrows(self, worksheet):
        """ Return the number of rows in a worksheet """
        return self.__sheets__[worksheet]['rows']
    def ncols(self, worksheet):
        """ Return the number of columns in a worksheet """
        return self.__sheets__[worksheet]['cols']
    def variables(self,worksheet):
        """ Returns a list of Column Names in the file,
            assuming a tabular format of course. """
        return self.__sheets__[worksheet]['variables']
    def __formatrow__(self, types, values, wanttupledate):
        """ Internal function used to clean up the incoming excel data """
        ##  Data Type Codes:
        ##  EMPTY 0
        ##  TEXT 1 a Unicode string 
        ##  NUMBER 2 float 
        ##  DATE 3 float 
        ##  BOOLEAN 4 int; 1 means TRUE, 0 means FALSE 
        ##  ERROR 5 
        import xlrd
        returnrow = []
        for i in range(len(types)):
            type,value = types[i],values[i]
            if type == 2:
                if value == int(value):
                    value = int(value)
            elif type == 3:
                datetuple = xlrd.xldate_as_tuple(value, self.__book__.datemode)
                if wanttupledate:
                    value = datetuple
                else:
                    # time only no date component
                    if datetuple[0] == 0 and datetuple[1] == 0 and \
                       datetuple[2] == 0: 
                        value = "%02d:%02d:%02d" % datetuple[3:]
                    # date only, no time
                    elif datetuple[3] == 0 and datetuple[4] == 0 and \
                         datetuple[5] == 0:
                        value = "%04d/%02d/%02d" % datetuple[:3]
                    else: # full date
                        value = "%04d/%02d/%02d %02d:%02d:%02d" % datetuple
            elif type == 5:
                value = xlrd.error_text_from_code[value]
            returnrow.append(value)
        return returnrow
    
def __iterlist__(excel, sheetname, tupledate):
    """ Function Used To Create the List Iterator """
    sheet = excel.__book__.sheet_by_name(sheetname)
    for row in range(excel.__sheets__[sheetname]['rows']):
        types,values = sheet.row_types(row),sheet.row_values(row)
        yield excel.__formatrow__(types, values, tupledate)

def __iterdict__(excel, sheetname, tupledate):
    """ Function Used To Create the Dictionary Iterator """
    sheet = excel.__book__.sheet_by_name(sheetname)
    for row in range(excel.__sheets__[sheetname]['firstrow'],\
                     excel.__sheets__[sheetname]['rows']):
        types,values = sheet.row_types(row),sheet.row_values(row)
        formattedrow = excel.__formatrow__(types, values, tupledate)
        # Pad a Short Row With Blanks if Needed
        for i in range(len(formattedrow),\
                       len(excel.__sheets__[sheetname]['variables'])):
            formattedrow.append('')
        yield dict(zip(excel.__sheets__[sheetname]['variables'],formattedrow))




2007-10-18 



ttkk1024 



·¢¼þÈË£º ??? ?? 
·¢ËÍʱ¼ä£º 2007-10-18  14:52:39 
ÊÕ¼þÈË£º python-chinese 
³­ËÍ£º 
Ö÷Ì⣺ [python-chinese]python²Ù×÷excel 
 
Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071018/6fa785af/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月19日 星期五 09:54

??? ?? clfff.peter在gmail.com
星期五 十月 19 09:54:01 HKT 2007

ÆäʵÎÒÒѾ­½«excelÖÐÄÚÈݶÁ³öÀ´ÁË£¬²»¹ýÔÚȡij¸öcellÖеÄʵ¼ÊÄÚÈÝʱÓöµ½ÁËÎÊÌ⣬´úÂëÈçÏ£º
 from win32com.client import Dispatch

xlApp = Dispatch("Excel.Application")
xlApp.Visible=1
doc=xlApp.Workbooks.Open('d:\\sample.xls')
sheet=doc.Sheets(2)
try:
    for i in range(1, 3, 1):
        s='raw(%d)' % i
        for j in range(1, 5, 1):
            cell=sheet.Cells(i,j)
            str1=str(cell)   * # ¼ÙÉ赱ǰcellÖÐÓз¨ÎÄ£¬ÕâÀï»áÓÐÒì³£
*            s+=str1.ljust(20)
        print s
finally:
    xlApp.Quit()

cellÖеÄÄÚÈÝÊǶàÓïÑԵģ¬µ«ÎÒÓÃstrת»»Ê±Êǽ«ÆäÖÐÄÚÈÝת»»Îªascii£¬ËùÒÔÓÐÎÊÌ⣬ÓÐûÓа취½«cellÄÚÈÝתΪһ¸ö

unicode×Ö·û´®Ñ½£¿£¿£¿

ÔÚ07-10-18£¬hechu <hcpython在gmail.com> дµÀ£º
>
> Èç¹ûÊÇ°Ñ Excel ×÷Ϊ³¬Ð¡ÐÍÊý¾Ý¿â£¬ÎÒÓÐʾÀý´úÂ룺
> ÒòΪһЩͬÊÂÍêÈ«²»¶®Êý¾Ý¿â£¬µ«ÊÇ»áÓûù±¾µÄ Excel Îļþ¡£
>
> ³ÌÐòµ÷Óà win32com ÐÞ¸ÄExcelÖеļǼ×ֶαê¼Ç¡£ÓÃ×÷×î»ù±¾µÄÊý¾Ý¿â¡£
> ÏÂÃæµÄ´úÂëÑÝʾÁ˶ÁÈ¡¡¢ÐÞ¸ÄExcelÎļþÖÐ[Sheet1]±íÖеļǼ¡£±í°üº¬"ºÅÂë"¡¢"״̬"Á½¸öÖÐÎÄ×ֶΡ£ÓÃÀ´¼Ç¼ÊÖ»úºÅÂëµÄ״̬µÄ¡£
> ×¢Òâ±íÃûÒ»¶¨ÒªÓÃ[]·½À¨ºÅÀ¨ÆðÀ´¡£
>
> code:
>   #!/usr/bin/python
> # -*- coding: gbk -*-
>
> import os.path
> import win32com.client
> import string
>
> class ExcelNumList:
>     def __init__(self, filename = None):
>         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
>         self.opened = False
>         return None
>
>     def open(self, filename):
>         del self.conn
>         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
>         self.opened = False
>
>         # print dsn
>         if os.path.exists(filename):
>             dsn = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;'    \
>                 'Extended Properties=Excel 8.0;'        \
>                 'Persist Security Info=False;'            \
>                 'Data Source=%s;'%filename
>             #print dsn
>             try:
>                 self.conn.Open(dsn)
>                 self.opened = True
>             except:
>                 self.opened = False
>         else:
>             return None
>
>     def close(self):
>         if self.opened:
>             try:
>                 self.conn.Close()
>             except:
>                 pass
>         #self.conn = None
>
>     def getNumList(self):
>         list = []
>         rs = win32com.client.Dispatch(r'ADODB.Recordset')
>         try:
>             rs.Open('SELECT * FROM [Sheet1$] WHERE ״̬ IS NULL', self.conn,
> 1, 3)
>         except:
>             return False
>
>         while not rs.EOF:
>             num = "%s"%int(rs.Fields["ºÅÂë"].Value)
>             list.append(num)
>             rs.MoveNext()
>         rs.Close()
>         del rs
>         return list
>
>     def updateNum(self, number, status=0):
>         sql = "UPDATE [Sheet1$] SET ״̬=%s WHERE ºÅÂë=%s"%(status, number)
>         self.conn.Execute(sql)
>
> ??? ?? дµÀ:
>
> Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
>  ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
> ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿
>
>
> ------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071019/96a3af94/attachment-0001.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月19日 星期五 10:00

limodou limodou在gmail.com
星期五 十月 19 10:00:06 HKT 2007

Ó¦¸ÃÒѾ­ÊÇunicodeÁË°É.Äã¿ÉÒÔʹÓÃprint type(obj)´òӡһϿ´¿´ÊDz»ÊÇunicode.

On 10/19/07, ??? ?? <clfff.peter在gmail.com> wrote:
>
> ÆäʵÎÒÒѾ­½«excelÖÐÄÚÈݶÁ³öÀ´ÁË£¬²»¹ýÔÚȡij¸öcellÖеÄʵ¼ÊÄÚÈÝʱÓöµ½ÁËÎÊÌ⣬´úÂëÈçÏ£º
>  from win32com.client import Dispatch
>
> xlApp = Dispatch("Excel.Application")
> xlApp.Visible=1
> doc=xlApp.Workbooks.Open('d:\\sample.xls')
> sheet=doc.Sheets(2)
> try:
>     for i in range(1, 3, 1):
>         s='raw(%d)' % i
>         for j in range(1, 5, 1):
>             cell=sheet.Cells(i,j)
>             str1=str(cell)   * # ¼ÙÉ赱ǰcellÖÐÓз¨ÎÄ£¬ÕâÀï»áÓÐÒì³£
> *            s+=str1.ljust(20)
>         print s
> finally:
>     xlApp.Quit()
>
> cellÖеÄÄÚÈÝÊǶàÓïÑԵģ¬µ«ÎÒÓÃstrת»»Ê±Êǽ«ÆäÖÐÄÚÈÝת»»Îªascii£¬ËùÒÔÓÐÎÊÌ⣬ÓÐûÓа취½«cellÄÚÈÝתΪһ¸ö
>
> unicode×Ö·û´®Ñ½£¿£¿£¿
>
> ÔÚ07-10-18£¬hechu <hcpython在gmail.com> дµÀ£º
> >
> > Èç¹ûÊÇ°Ñ Excel ×÷Ϊ³¬Ð¡ÐÍÊý¾Ý¿â£¬ÎÒÓÐʾÀý´úÂ룺
> > ÒòΪһЩͬÊÂÍêÈ«²»¶®Êý¾Ý¿â£¬µ«ÊÇ»áÓûù±¾µÄ Excel Îļþ¡£
> >
> > ³ÌÐòµ÷Óà win32com ÐÞ¸ÄExcelÖеļǼ×ֶαê¼Ç¡£ÓÃ×÷×î»ù±¾µÄÊý¾Ý¿â¡£
> > ÏÂÃæµÄ´úÂëÑÝʾÁ˶ÁÈ¡¡¢ÐÞ¸ÄExcelÎļþÖÐ[Sheet1]±íÖеļǼ¡£±í°üº¬"ºÅÂë"¡¢"״̬"Á½¸öÖÐÎÄ×ֶΡ£ÓÃÀ´¼Ç¼ÊÖ»úºÅÂëµÄ״̬µÄ¡£
> > ×¢Òâ±íÃûÒ»¶¨ÒªÓÃ[]·½À¨ºÅÀ¨ÆðÀ´¡£
> >
> > code:
> >   #!/usr/bin/python
> > # -*- coding: gbk -*-
> >
> > import os.path
> > import win32com.client
> > import string
> >
> > class ExcelNumList:
> >     def __init__(self, filename = None):
> >         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
> >         self.opened = False
> >         return None
> >
> >     def open(self, filename):
> >         del self.conn
> >         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
> >         self.opened = False
> >
> >         # print dsn
> >         if os.path.exists(filename):
> >             dsn = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;'    \
> >                 'Extended Properties=Excel 8.0;'        \
> >                 'Persist Security Info=False;'            \
> >                 'Data Source=%s;'%filename
> >             #print dsn
> >             try:
> >                 self.conn.Open(dsn)
> >                 self.opened = True
> >             except:
> >                 self.opened = False
> >         else:
> >             return None
> >
> >     def close(self):
> >         if self.opened:
> >             try:
> >                 self.conn.Close()
> >             except:
> >                 pass
> >         #self.conn = None
> >
> >     def getNumList(self):
> >         list = []
> >         rs = win32com.client.Dispatch(r'ADODB.Recordset')
> >         try:
> >             rs.Open('SELECT * FROM [Sheet1$] WHERE ״̬ IS NULL',
> > self.conn, 1, 3)
> >         except:
> >             return False
> >
> >         while not rs.EOF:
> >             num = "%s"%int(rs.Fields["ºÅÂë"].Value)
> >             list.append(num)
> >             rs.MoveNext()
> >         rs.Close()
> >         del rs
> >         return list
> >
> >     def updateNum(self, number, status=0):
> >         sql = "UPDATE [Sheet1$] SET ״̬=%s WHERE ºÅÂë=%s"%(status, number)
> >         self.conn.Execute(sql)
> >
> > ??? ?? дµÀ:
> >
> > Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
> >  ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
> > ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿
> >
> >
> > ------------------------------
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
I like python!
UliPad <>: http://code.google.com/p/ulipad/
meide <>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071019/9f743fd2/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月19日 星期五 10:09

faitherQ faitherQ在163.com
星期五 十月 19 10:09:42 HKT 2007

ÐÖµÜÄãÊDz»ÊÇÏë¸ã¶à¹úÓïÑÔ°¡¡£ ^_^¡£ÎÒ¸ãÁ˺öà´ÎÁË¡£

cell=sheet.Cells(i,j)

¶Á³öµÄÒѾ­ÊÇunicodeÁË¡£

¿´¿´ÏÂÃæµÄ´úÂ룬 ÔÚÄãÒª¶ÁµÄµ¥Ôª¸ñ£¬ÊäÈë¡®ÎÒ¡¯


from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible=1
doc=xlApp.Workbooks.Open('h:/a/b.xls')
sheet=doc.Sheets(1)

for i in range(1, 3, 1):
    s='raw(%d)' % i 
    for j in range(1, 5, 1):
        cell=sheet.Cells(i,j)        
        print cell.value
        print hex(ord(cell.value))
        #str1=str(cell)    
        #s+=str1.ljust(20)
    print s







faitherQ
2007-10-19



·¢¼þÈË£º ??? ??
·¢ËÍʱ¼ä£º 2007-10-19 09:54:17
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³­ËÍ£º 
Ö÷Ì⣺ Re: [python-chinese]python²Ù×÷excel

ÆäʵÎÒÒѾ­½«excelÖÐÄÚÈݶÁ³öÀ´ÁË£¬²»¹ýÔÚȡij¸öcellÖеÄʵ¼ÊÄÚÈÝʱÓöµ½ÁËÎÊÌ⣬´úÂëÈçÏ£º
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible=1
doc=xlApp.Workbooks.Open('d:\\sample.xls')
sheet=doc.Sheets(2)
try:
    for i in range(1, 3, 1):
        s='raw(%d)' % i 
        for j in range(1, 5, 1):
            cell=sheet.Cells(i,j)
            str1=str(cell)    # ¼ÙÉ赱ǰcellÖÐÓз¨ÎÄ£¬ÕâÀï»áÓÐÒì³£
            s+=str1.ljust(20)
        print s
finally:
    xlApp.Quit()
cellÖеÄÄÚÈÝÊǶàÓïÑԵģ¬µ«ÎÒÓÃstrת»»Ê±Êǽ«ÆäÖÐÄÚÈÝת»»Îªascii£¬ËùÒÔÓÐÎÊÌ⣬ÓÐûÓа취½«cellÄÚÈÝתΪһ¸ö
unicode×Ö·û´®Ñ½£¿£¿£¿


ÔÚ07-10-18£¬hechu <hcpython在gmail.com> дµÀ£º 
Èç¹ûÊÇ°Ñ Excel ×÷Ϊ³¬Ð¡ÐÍÊý¾Ý¿â£¬ÎÒÓÐʾÀý´úÂ룺
ÒòΪһЩͬÊÂÍêÈ«²»¶®Êý¾Ý¿â£¬µ«ÊÇ»áÓûù±¾µÄ Excel Îļþ¡£

³ÌÐòµ÷Óà win32com ÐÞ¸ÄExcelÖеļǼ×ֶαê¼Ç¡£ÓÃ×÷×î»ù±¾µÄÊý¾Ý¿â¡£
ÏÂÃæµÄ´úÂëÑÝʾÁ˶ÁÈ¡¡¢ÐÞ¸ÄExcelÎļþÖÐ[Sheet1]±íÖеļǼ¡£±í°üº¬"ºÅÂë"¡¢"״̬"Á½¸öÖÐÎÄ×ֶΡ£ÓÃÀ´¼Ç¼ÊÖ»úºÅÂëµÄ״̬µÄ¡£ 
×¢Òâ±íÃûÒ»¶¨ÒªÓÃ[]·½À¨ºÅÀ¨ÆðÀ´¡£

code:
#!/usr/bin/python
# -*- coding: gbk -*-

import os.path
import win32com.client
import string

class ExcelNumList:
    def __init__(self, filename = None): 
        self.conn = win32com.client.Dispatch(r'ADODB.Connection')
        self.opened = False
        return None
    
    def open(self, filename):
        del self.conn
        self.conn = win32com.client.Dispatch(r'ADODB.Connection')
        self.opened = False
        
        # print dsn
        if os.path.exists(filename):
            dsn = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;'    \ 
                'Extended Properties=Excel 8.0;'        \
                'Persist Security Info=False;'            \
                'Data Source=%s;'%filename
            #print dsn
            try:
                self.conn.Open(dsn)
                self.opened = True
            except:
                self.opened = False
        else:
            return None

    def close(self): 
        if self.opened:
            try:
                self.conn.Close()
            except:
                pass
        #self.conn = None

    def getNumList(self):
        list = []
        rs = win32com.client.Dispatch(r'ADODB.Recordset')
        try:
            rs.Open('SELECT * FROM [Sheet1$] WHERE ״̬ IS NULL', self.conn, 1, 3)
        except:
            return False

        while not rs.EOF:
            num = "%s"%int(rs.Fields["ºÅÂë"].Value)
            list.append(num)
            rs.MoveNext()
        rs.Close()
        del rs
        return list

    def updateNum(self, number, status=0): 
        sql = "UPDATE [Sheet1$] SET ״̬=%s WHERE ºÅÂë=%s"%(status, number)
        self.conn.Execute(sql)


??? ?? дµÀ: 
Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿


_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese

_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn 
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to   python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071019/b4e2b1ed/attachment-0001.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月19日 星期五 13:47

??? ?? clfff.peter在gmail.com
星期五 十月 19 13:47:04 HKT 2007

¹þ¹þ£¬¾ÍÊǶà¹úÓïÑÔ£¬ÕæÊÇÖªÒôѽ¡£
û´í£¬Ïñlimodou˵µÄ£¬ÊÇunicodeÀàÐÍ¡£
"ÎÒ"Ò²ÄÜÏÔʾ³öÀ´£¬µ«ÊÇ·¨ÎÄÈ´²»ÄÜÏÔʾ£¬µ±ÏÔʾ·¨ÎÄ£¨print cell.Value£©Ê±£¬Òì³££º
Traceback (most recent call last):
  File "D:\test\python\python_serials\excel_2.py", line 18, in 
    print cell.Value
UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in
position 0: ordinal not in range(128)

*£¨ÒÔÉ϶¼ÊÇÔÚwindowsµÄcommand line´°¿Ú£¨dos´°¿Ú£©ÖÐÖ´ÐÐ.pyʱµÄÒì³££¬ÔÚPythonWinÖв»»áÓÐÒì³££¬¿´À´ÊÇwindows
command line´°¿ÚµÄÎÊÌ⣬¾Í²»ÀÍ´ó¼ÒÁË£¬Ð»Ð»**£©*


ÔÚ07-10-19£¬faitherQ <faitherQ在163.com> дµÀ£º
>
>  ÐÖµÜÄãÊDz»ÊÇÏë¸ã¶à¹úÓïÑÔ°¡¡£ ^_^¡£ÎÒ¸ãÁ˺öà´ÎÁË¡£
>
> cell=sheet.Cells(i,j)
>
> ¶Á³öµÄÒѾ­ÊÇunicodeÁË¡£
>
> ¿´¿´ÏÂÃæµÄ´úÂ룬 ÔÚÄãÒª¶ÁµÄµ¥Ôª¸ñ£¬ÊäÈë'ÎÒ'
>
>
> from win32com.client import Dispatch
> xlApp = Dispatch("Excel.Application")
> xlApp.Visible=1
> doc=xlApp.Workbooks.Open('h:/a/b.xls')
> sheet=doc.Sheets(1)
>
> for i in range(1, 3, 1):
>     s='raw(%d)' % i
>     for j in range(1, 5, 1):
>         cell=sheet.Cells(i,j)
>         print cell.value
>         print hex(ord(cell.value))
>         #str1=str(cell)
>         #s+=str1.ljust(20)
>     print s
>
>
>
>  ------------------------------
>  faitherQ
> 2007-10-19
>  ------------------------------
>  *·¢¼þÈË£º* ??? ??
> *·¢ËÍʱ¼ä£º* 2007-10-19 09:54:17
> *ÊÕ¼þÈË£º* python-chinese在lists.python.cn
> *³­ËÍ£º*
> *Ö÷Ì⣺* Re: [python-chinese]python²Ù×÷excel
>
>  ÆäʵÎÒÒѾ­½«excelÖÐÄÚÈݶÁ³öÀ´ÁË£¬²»¹ýÔÚȡij¸öcellÖеÄʵ¼ÊÄÚÈÝʱÓöµ½ÁËÎÊÌ⣬´úÂëÈçÏ£º
>  from win32com.client import Dispatch
>
> xlApp = Dispatch("Excel.Application")
> xlApp.Visible=1
> doc=xlApp.Workbooks.Open('d:\\sample.xls')
> sheet=doc.Sheets(2)
> try:
>     for i in range(1, 3, 1):
>         s='raw(%d)' % i
>         for j in range(1, 5, 1):
>             cell=sheet.Cells(i,j)
>             str1=str(cell)   * # ¼ÙÉ赱ǰcellÖÐÓз¨ÎÄ£¬ÕâÀï»áÓÐÒì³£
> *            s+=str1.ljust(20)
>         print s
> finally:
>     xlApp.Quit()
>
> cellÖеÄÄÚÈÝÊǶàÓïÑԵģ¬µ«ÎÒÓÃstrת»»Ê±Êǽ«ÆäÖÐÄÚÈÝת»»Îªascii£¬ËùÒÔÓÐÎÊÌ⣬ÓÐûÓа취½«cellÄÚÈÝתΪһ¸ö
>
> unicode×Ö·û´®Ñ½£¿£¿£¿
>
> ÔÚ07-10-18£¬hechu <hcpython在gmail.com> дµÀ£º
> >
> > Èç¹ûÊÇ°Ñ Excel ×÷Ϊ³¬Ð¡ÐÍÊý¾Ý¿â£¬ÎÒÓÐʾÀý´úÂ룺
> > ÒòΪһЩͬÊÂÍêÈ«²»¶®Êý¾Ý¿â£¬µ«ÊÇ»áÓûù±¾µÄ Excel Îļþ¡£
> >
> > ³ÌÐòµ÷Óà win32com ÐÞ¸ÄExcelÖеļǼ×ֶαê¼Ç¡£ÓÃ×÷×î»ù±¾µÄÊý¾Ý¿â¡£
> > ÏÂÃæµÄ´úÂëÑÝʾÁ˶ÁÈ¡¡¢ÐÞ¸ÄExcelÎļþÖÐ[Sheet1]±íÖеļǼ¡£±í°üº¬"ºÅÂë"¡¢"״̬"Á½¸öÖÐÎÄ×ֶΡ£ÓÃÀ´¼Ç¼ÊÖ»úºÅÂëµÄ״̬µÄ¡£
> > ×¢Òâ±íÃûÒ»¶¨ÒªÓÃ[]·½À¨ºÅÀ¨ÆðÀ´¡£
> >
> > code:
> >   #!/usr/bin/python
> > # -*- coding: gbk -*-
> >
> > import os.path
> > import win32com.client
> > import string
> >
> > class ExcelNumList:
> >     def __init__(self, filename = None):
> >         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
> >         self.opened = False
> >         return None
> >
> >     def open(self, filename):
> >         del self.conn
> >         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
> >         self.opened = False
> >
> >         # print dsn
> >         if os.path.exists(filename):
> >             dsn = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;'    \
> >                 'Extended Properties=Excel 8.0;'        \
> >                 'Persist Security Info=False;'            \
> >                 'Data Source=%s;'%filename
> >             #print dsn
> >             try:
> >                 self.conn.Open(dsn)
> >                 self.opened = True
> >             except:
> >                 self.opened = False
> >         else:
> >             return None
> >
> >     def close(self):
> >         if self.opened:
> >             try:
> >                 self.conn.Close()
> >             except:
> >                 pass
> >         #self.conn = None
> >
> >     def getNumList(self):
> >         list = []
> >         rs = win32com.client.Dispatch(r'ADODB.Recordset')
> >         try:
> >             rs.Open('SELECT * FROM [Sheet1$] WHERE ״̬ IS NULL',
> > self.conn, 1, 3)
> >         except:
> >             return False
> >
> >         while not rs.EOF:
> >             num = "%s"%int(rs.Fields["ºÅÂë"].Value)
> >             list.append(num)
> >             rs.MoveNext()
> >         rs.Close()
> >         del rs
> >         return list
> >
> >     def updateNum(self, number, status=0):
> >         sql = "UPDATE [Sheet1$] SET ״̬=%s WHERE ºÅÂë=%s"%(status, number)
> >         self.conn.Execute(sql)
> >
> > ??? ?? дµÀ:
> >
> > Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
> >  ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
> > ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿
> >
> >
> > ------------------------------
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071019/ec98c61e/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月19日 星期五 13:52

limodou limodou在gmail.com
星期五 十月 19 13:52:24 HKT 2007

On 10/19/07, ??? ?? <clfff.peter在gmail.com> wrote:
>
> ¹þ¹þ£¬¾ÍÊǶà¹úÓïÑÔ£¬ÕæÊÇÖªÒôѽ¡£
> û´í£¬Ïñlimodou˵µÄ£¬ÊÇunicodeÀàÐÍ¡£
> "ÎÒ"Ò²ÄÜÏÔʾ³öÀ´£¬µ«ÊÇ·¨ÎÄÈ´²»ÄÜÏÔʾ£¬µ±ÏÔʾ·¨ÎÄ£¨print cell.Value£©Ê±£¬Òì³££º
> Traceback (most recent call last):
>   File "D:\test\python\python_serials\excel_2.py", line 18, in 
>     print cell.Value
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in
> position 0: ordinal not in range(128)
>
> *£¨ÒÔÉ϶¼ÊÇÔÚwindowsµÄcommand line´°¿Ú£¨dos´°¿Ú£©ÖÐÖ´ÐÐ.pyʱµÄÒì³££¬ÔÚPythonWinÖв»»áÓÐÒì³££¬¿´À´ÊÇwindows
> command line´°¿ÚµÄÎÊÌ⣬¾Í²»ÀÍ´ó¼ÒÁË£¬Ð»Ð»**£©*
>
>
>
ÖµÒѾ­ÊÇÈ¡³öÀ´ÁË,Ö»²»¹ý´òÓ¡µÄ»°ÒòΪÃüÁîÐб¾ÉíÊÇÓбàÂëµÄ,ËùÒÔÖ±½Ó´òÓ¡Ò»¸öunicodeÊDz»ÐеÄ,
ҪתΪÓëÃüÁîÐÐÏàÒ»ÖµıàÂë²Å¿ÉÒÔ.Ò»¸ö¼òµ¥µÄ·½·¨ÊÇתΪutf-8´æµ½Ò»¸öÎļþÖÐ,È»ºóʹÓÃÒ»¸ö¿ÉÒÔʶ±ðutf-8µÄ±à¼­Æ÷²é¿´¾Í¿ÉÒÔÁË.

-- 
I like python!
UliPad <>: http://code.google.com/p/ulipad/
meide <>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071019/6553e9cf/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月19日 星期五 13:59

Cyril_Gmail terry6394在gmail.com
星期五 十月 19 13:59:54 HKT 2007

挺不错,收藏

On 10/18/07, hechu <hcpython at gmail.com> wrote:
>
>  如果是把 Excel 作为超小型数据库,我有示例代码:
> 因为一些同事完全不懂数据库,但是会用基本的 Excel 文件。
>
> 程序调用 win32com 修改Excel中的记录字段标记。用作最基本的数据库。
> 下面的代码演示了读取、修改Excel文件中[Sheet1]表中的记录。表包含"号码"、"状态"两个中文字段。用来记录手机号码的状态的。
> 注意表名一定要用[]方括号括起来。
>
> code:
>   #!/usr/bin/python
> # -*- coding: gbk -*-
>
> import os.path
> import win32com.client
> import string
>
> class ExcelNumList:
>     def __init__(self, filename = None):
>         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
>         self.opened = False
>         return None
>
>     def open(self, filename):
>         del self.conn
>         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
>         self.opened = False
>
>         # print dsn
>         if os.path.exists(filename):
>             dsn = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;'    \
>                 'Extended Properties=Excel 8.0;'        \
>                 'Persist Security Info=False;'            \
>                 'Data Source=%s;'%filename
>             #print dsn
>             try:
>                 self.conn.Open(dsn)
>                 self.opened = True
>             except:
>                 self.opened = False
>         else:
>             return None
>
>     def close(self):
>         if self.opened:
>             try:
>                 self.conn.Close()
>             except:
>                 pass
>         #self.conn = None
>
>     def getNumList(self):
>         list = []
>         rs = win32com.client.Dispatch(r'ADODB.Recordset')
>         try:
>             rs.Open('SELECT * FROM [Sheet1$] WHERE 状态 IS NULL', self.conn,
> 1, 3)
>         except:
>             return False
>
>         while not rs.EOF:
>             num = "%s"%int(rs.Fields["号码"].Value)
>             list.append(num)
>             rs.MoveNext()
>         rs.Close()
>         del rs
>         return list
>
>     def updateNum(self, number, status=0):
>         sql = "UPDATE [Sheet1$] SET 状态=%s WHERE 号码=%s"%(status, number)
>         self.conn.Execute(sql)
>
> ??? ?? 写道:
>
> 想做个脚本处理excel文件,
>  网络上搜到的都比较零散,是个别几个api的调用示例,
> 有没有比较完整的文档呀?
>
>
> ------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20071019/6e4bab70/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月19日 星期五 14:07

faitherQ faitherQ在163.com
星期五 十月 19 14:07:32 HKT 2007

 Äã²»ÒªÓÃprint value ¾ÍokÁË£¬ÎÒÖ»ÊǸøÄã×öÒ»¸öÑÝʾ¶øÒÑ¡£

Ö±½ÓÓà ord(cell.value)¡£ Õâ¸ö¾ÍÊÇunicodeÖµÁË£¬ÓÐÁËÕâ¸ö£¬ÄãÏë¸Éô¾Í¸ÉÁË¡£ ±ÈÈçÒÔijÖÖ¸ñʽÊä³öµ½Îļþ¡£^_^

 






faitherQ
2007-10-19



·¢¼þÈË£º ??? ??
·¢ËÍʱ¼ä£º 2007-10-19 13:47:46
ÊÕ¼þÈË£º python-chinese在lists.python.cn
³­ËÍ£º 
Ö÷Ì⣺ Re: [python-chinese]python²Ù×÷excel

¹þ¹þ£¬¾ÍÊǶà¹úÓïÑÔ£¬ÕæÊÇÖªÒôѽ¡£
û´í£¬Ïñlimodou˵µÄ£¬ÊÇunicodeÀàÐÍ¡£
"ÎÒ"Ò²ÄÜÏÔʾ³öÀ´£¬µ«ÊÇ·¨ÎÄÈ´²»ÄÜÏÔʾ£¬µ±ÏÔʾ·¨ÎÄ£¨print cell.Value£©Ê±£¬Òì³££º
Traceback (most recent call last):
  File "D:\test\python\python_serials\excel_2.py", line 18, in 
    print cell.Value
UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in position 0: ordinal not in range(128) 

£¨ÒÔÉ϶¼ÊÇÔÚwindowsµÄcommand line´°¿Ú£¨dos´°¿Ú£©ÖÐÖ´ÐÐ.pyʱµÄÒì³££¬ÔÚPythonWinÖв»»áÓÐÒì³££¬¿´À´ÊÇwindows command line´°¿ÚµÄÎÊÌ⣬¾Í²»ÀÍ´ó¼ÒÁË£¬Ð»Ð»£©

 
ÔÚ07-10-19£¬faitherQ <faitherQ在163.com> дµÀ£º 
ÐÖµÜÄãÊDz»ÊÇÏë¸ã¶à¹úÓïÑÔ°¡¡£ ^_^¡£ÎÒ¸ãÁ˺öà´ÎÁË¡£

cell=sheet.Cells(i,j)

¶Á³öµÄÒѾ­ÊÇunicodeÁË¡£

¿´¿´ÏÂÃæµÄ´úÂ룬 ÔÚÄãÒª¶ÁµÄµ¥Ôª¸ñ£¬ÊäÈë'ÎÒ'


from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible=1
doc=xlApp.Workbooks.Open('h:/a/b.xls') 
sheet=doc.Sheets(1)

for i in range(1, 3, 1):
    s='raw(%d)' % i 
    for j in range(1, 5, 1):
        cell=sheet.Cells(i,j)        
        print cell.value
        print hex(ord(cell.value))
        #str1=str(cell)    
        #s+=str1.ljust(20)
    print s


 




faitherQ
2007-10-19



·¢¼þÈË£º ??? ??
·¢ËÍʱ¼ä£º 2007-10-19 09:54:17
ÊÕ¼þÈË£º python-chinese在lists.python.cn 
³­ËÍ£º 
Ö÷Ì⣺ Re: [python-chinese]python²Ù×÷excel

ÆäʵÎÒÒѾ­½«excelÖÐÄÚÈݶÁ³öÀ´ÁË£¬²»¹ýÔÚȡij¸öcellÖеÄʵ¼ÊÄÚÈÝʱÓöµ½ÁËÎÊÌ⣬´úÂëÈçÏ£º
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
xlApp.Visible=1
doc=xlApp.Workbooks.Open('d:\\sample.xls')
sheet=doc.Sheets(2)
try:
    for i in range(1, 3, 1):
        s='raw(%d)' % i 
        for j in range(1, 5, 1):
            cell=sheet.Cells(i,j)
            str1=str(cell)    # ¼ÙÉ赱ǰcellÖÐÓз¨ÎÄ£¬ÕâÀï»áÓÐÒì³£
            s+=str1.ljust(20)
        print s
finally:
    xlApp.Quit()
cellÖеÄÄÚÈÝÊǶàÓïÑԵģ¬µ«ÎÒÓÃstrת»»Ê±Êǽ«ÆäÖÐÄÚÈÝת»»Îªascii£¬ËùÒÔÓÐÎÊÌ⣬ÓÐûÓа취½«cellÄÚÈÝתΪһ¸ö
unicode×Ö·û´®Ñ½£¿£¿£¿


ÔÚ07-10-18£¬hechu <hcpython在gmail.com> дµÀ£º 
Èç¹ûÊÇ°Ñ Excel ×÷Ϊ³¬Ð¡ÐÍÊý¾Ý¿â£¬ÎÒÓÐʾÀý´úÂ룺
ÒòΪһЩͬÊÂÍêÈ«²»¶®Êý¾Ý¿â£¬µ«ÊÇ»áÓûù±¾µÄ Excel Îļþ¡£

³ÌÐòµ÷Óà win32com ÐÞ¸ÄExcelÖеļǼ×ֶαê¼Ç¡£ÓÃ×÷×î»ù±¾µÄÊý¾Ý¿â¡£
ÏÂÃæµÄ´úÂëÑÝʾÁ˶ÁÈ¡¡¢ÐÞ¸ÄExcelÎļþÖÐ[Sheet1]±íÖеļǼ¡£±í°üº¬"ºÅÂë"¡¢"״̬"Á½¸öÖÐÎÄ×ֶΡ£ÓÃÀ´¼Ç¼ÊÖ»úºÅÂëµÄ״̬µÄ¡£ 
×¢Òâ±íÃûÒ»¶¨ÒªÓÃ[]·½À¨ºÅÀ¨ÆðÀ´¡£

code:
#!/usr/bin/python
# -*- coding: gbk -*-

import os.path
import win32com.client
import string

class ExcelNumList:
    def __init__(self, filename = None): 
        self.conn = win32com.client.Dispatch(r'ADODB.Connection')
        self.opened = False
        return None
    
    def open(self, filename):
        del self.conn
        self.conn = win32com.client.Dispatch(r'ADODB.Connection')
        self.opened = False
        
        # print dsn
        if os.path.exists(filename):
            dsn = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;'    \ 
                'Extended Properties=Excel 8.0;'        \
                'Persist Security Info=False;'            \
                'Data Source=%s;'%filename
            #print dsn
            try:
                self.conn.Open(dsn)
                self.opened = True
            except:
                self.opened = False
        else:
            return None

    def close(self): 
        if self.opened:
            try:
                self.conn.Close()
            except:
                pass
        #self.conn = None

    def getNumList(self):
        list = []
        rs = win32com.client.Dispatch(r'ADODB.Recordset')
        try:
            rs.Open('SELECT * FROM [Sheet1$] WHERE ״̬ IS NULL', self.conn, 1, 3)
        except:
            return False

        while not rs.EOF:
            num = "%s"%int(rs.Fields["ºÅÂë"].Value)
            list.append(num)
            rs.MoveNext()
        rs.Close()
        del rs
        return list

    def updateNum(self, number, status=0): 
        sql = "UPDATE [Sheet1$] SET ״̬=%s WHERE ºÅÂë=%s"%(status, number)
        self.conn.Execute(sql)


??? ?? дµÀ: 
Ïë×ö¸ö½Å±¾´¦ÀíexcelÎļþ£¬
ÍøÂçÉÏËѵ½µÄ¶¼±È½ÏÁãÉ¢£¬ÊǸö±ð¼¸¸öapiµÄµ÷ÓÃʾÀý£¬
ÓÐûÓбȽÏÍêÕûµÄÎĵµÑ½£¿


_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese

_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn 
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to   python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese




_______________________________________________
python-chinese
Post: send python-chinese在lists.python.cn
Subscribe: send subscribe to python-chinese-request在lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20071019/21470a77/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月19日 星期五 16:05

hechu hcpython在gmail.com
星期五 十月 19 16:05:54 HKT 2007

一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20071019/e23b7a69/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年10月21日 星期日 22:10

Cyril.Liu terry6394在gmail.com
星期日 十月 21 22:10:53 HKT 2007

的确,我最近在处理doc,在python的多线程下去打开多个word文件是会抛出异常的。

On 10/19/07, hechu <hcpython at gmail.com> wrote:
>
>  呵呵,有人觉得不错就再补充两句。
> 这个不是线程安全的,所以多线程操作的时候要用互斥量保护起来。
>
> Cyril_Gmail 写道:
>
> 挺不错,收藏
>
> On 10/18/07, hechu <hcpython at gmail.com> wrote:
> >
> > 如 果是把 Excel 作为超小型数据库,我有示例代码:
> > 因为一些同事完全不懂数据库,但是会用基本的 Excel 文件。
> >
> > 程序调用 win32com 修改Excel中的记录字段标记。用作最基本的数据库。
> > 下面的代码演示了读取、修改Excel文件中[Sheet1]表中的记录。表包含"号码"、"状态"两个中文字段。用来记录手机号码的状态的。
> > 注意表名一定要用[]方括号括起来。
> >
> > code:
> >
> >   #!/usr/bin/python
> > # -*- coding: gbk -*-
> >
> > import os.path
> > import win32com.client
> > import string
> >
> > class ExcelNumList:
> >     def __init__(self, filename = None):
> >         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
> >         self.opened = False
> >         return None
> >
> >     def open(self, filename):
> >         del self.conn
> >         self.conn = win32com.client.Dispatch(r'ADODB.Connection')
> >         self.opened = False
> >
> >         # print dsn
> >         if os.path.exists(filename):
> >             dsn = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;'    \
> >                 'Extended Properties=Excel 8.0;'        \
> >                 'Persist Security Info=False;'            \
> >                 'Data Source=%s;'%filename
> >             #print dsn
> >             try:
> >                 self.conn.Open(dsn)
> >                 self.opened = True
> >             except:
> >                 self.opened = False
> >         else:
> >             return None
> >
> >     def close(self):
> >         if self.opened:
> >             try:
> >                 self.conn.Close()
> >             except:
> >                 pass
> >         #self.conn = None
> >
> >     def getNumList(self):
> >         list = []
> >         rs = win32com.client.Dispatch(r'ADODB.Recordset')
> >         try:
> >             rs.Open('SELECT * FROM [Sheet1$] WHERE 状态 IS NULL',
> > self.conn, 1, 3)
> >         except:
> >             return False
> >
> >         while not rs.EOF:
> >             num = "%s"%int(rs.Fields["号码"].Value)
> >             list.append(num)
> >             rs.MoveNext()
> >         rs.Close()
> >         del rs
> >         return list
> >
> >     def updateNum(self, number, status=0):
> >         sql = "UPDATE [Sheet1$] SET 状态=%s WHERE 号码=%s"%(status, number)
> >         self.conn.Execute(sql)
> >
> > ??? ?? 写道:
> >
> > 想做个脚本处理excel文件,
> >  网络上搜到的都比较零散,是个别几个api的调用示例,
> > 有没有比较完整的文档呀?
> >
> >
> > ------------------------------
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> >
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
> ------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20071021/f9c6b587/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号