2013-04-02 95 views
0

我有一个简单的任务 - 遍历excel文件的列表,并用其他词替换所有单词的所有出现。使用pywin32与Excel进行交互

找不到如何使用pywin32库。即我找不到任何明确的文档或示例如何与Excel进行交互。

我想迭代excel文档中的所有工作表,并将'111'替换为'222'。

我用下面的代码:

def searchexcel(): 
    excel = win32.gencache.EnsureDispatch('Excel.Application') 
    excel.Visible = False 
    for infile in glob.glob(os.path.join('', '*.xls')): 
     print infile 
     e = excel.Workbooks.Open(os.getcwd()+'\\'+infile) 
     sh = e.Sheets() 
     for sheet in e: 
      sheet.Replace('111', '222', win32.constants.xlWhole) 
     e.Save() 
    excel.Application.Quit() 

但它不能正常工作。

+1

怎么样'sheet.Cells.Replace ...'? –

+0

就是这样!现在它工作了! – annndrey

回答

1

因为它被张贴在评论中,我应该使用sheet.Cells.Replace