Python is a dynamically and strongly typed programming language that encourages readability.
1.链式操作符,要注意执行顺序问题,方法是加括号。
12345
>>>x=5>>>10<x<20False>>>x<10<x*10<100True
2.debug regex
12345678910
>>>re.compile(""" ^ # start of a line \[font # the font tag (?:=(?P<size> # optional [font=+size] [-+][0-9]{1,2} # size specification ))? \] # end of tag (.*?) # text between the tags \[/font\] # end of the tag """,re.DEBUG|re.VERBOSE|re.DOTALL)
另附name match:
1234
>>>p=re.compile(r'(?P<word>\b\w+\b)')>>>m=p.search('(((( Lots of punctuation )))')>>>m.group('word')'Lots'
#!/usr/bin/env python# -*- coding: utf-8 -*-# Last Update:'''covert many excel files to one word file'''__revision__='0.1'__author__='lxd'fromwin32com.clientimportDispatchimportoswdPageBreak=7defisExcelFile(name):returnos.path.isfile(name)andos.path.splitext(name)[1]=='.xls'deffilesInDir(dir):#generate absolute paths of files in a directoryforroot,dirs,filesinos.walk(dir):forfileinfiles:yieldos.path.join(root,file)deffindExcels(names):fornameinnames:ifisExcelFile(name):yieldnameelse:forfileinfilesInDir(name):ifisExcelFile(file):yieldfileclassExcelToWord(object):def__init__(self,excelNames,wordName):self._createExcel()self.excelNames=findExcels(excelNames)self._createWord()self.word.Documents.Open(wordName)def_createExcel(self):self.excel=Dispatch('Excel.Application')#self.excel.Visible = Trueself.excel.DisplayAlerts=Falsedef_createWord(self):self.word=Dispatch('Word.Application')self.word.Visible=Trueself.word.DisplayAlerts=Falsedefprocess(self):forexcelNameinself.excelNames:print'coverting',excelNameself.excel.Workbooks.Open(excelName)foriinrange(self.excel.Worksheets.count):self.excel.Worksheets(i+1).Select()#start from 1self._copy()self._paste()self.excel.Workbooks.Close()self.excel.Quit()print'covert is over'def_copy(self):# select all contents in one sheet and copyself.excel.Cells.Select()self.excel.Selection.Copy()def_paste(self):#paste to one word file and add a page breakself.word.Selection.Paste()#self.word.Selection.TypeParagraph()self.word.Selection.InsertBreak(wdPageBreak)if__name__=='__main__':excelNames=[u'c:\\users\\lxd\\desktop\\文档',u'c:\\users\\lxd\\desktop\\汇总.xls',]wordName=r'c:\users\lxd\desktop\1.doc'e=ExcelToWord(excelNames,wordName)e.process()
fromgoogle.appengine.extimportwebappregister=webapp.template.create_template_register()deftruncate(value,maxsize,stopper='...'):""" truncates a string to a given maximum size and appends the stopper if needed """stoplen=len(stopper)iflen(value)>maxsizeandmaxsize>stoplen:returnvalue[:(maxsize-stoplen)]+stopperelse:returnvalue[:maxsize]register.filter(truncate)