Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用xlrd和xlwt修改Excel_Python_Python 2.7 - Fatal编程技术网

Python 使用xlrd和xlwt修改Excel

Python 使用xlrd和xlwt修改Excel,python,python-2.7,Python,Python 2.7,我正在从现有excel创建新的excel。在我的旧excel中有三张3页,分别是第一张,第二张,第三张。我能够在新的excel中成功复制第一张工作表的内容,这是基于某些条件的,即我的代码在之前,而索引

我正在从现有excel创建新的excel。在我的旧excel中有三张
3页
,分别是
第一张
第二张
第三张
。我能够在新的excel中成功复制第一张工作表的内容,这是基于某些条件的,即我的代码在
之前,而索引<3:
工作得非常好。对于工作表
第二张
第三张
我想复制它们,就像它们在
原始excel工作表中一样。但是当第二张
图纸开始复制时,我得到一个错误:-

sheet1 = w.add_sheet('Info')
File "/usr/local/lib/python2.7/site-packages/xlwt/Workbook.py", line 331, in add_sheet
raise Exception("duplicate worksheet name %r" % sheetname) 
我的代码如下:-

newexcel = "newexcel.xls"
    count = 0
    wb = xlrd.open_workbook('/home/sam/myexcel.xls')
    w = Workbook()
    sheet = w.add_sheet('Input_Resource')
    index = 0
    s = wb.sheet_by_index(index)
    if index < 1 :
        index =+ 1
        for row in range(s.nrows):
            coln =0
            val = s.cell(row,coln).value
            if val in MissingId :
                mylist.append(val)
                count += 1
            else:
                for col in range(s.ncols):
                    val = s.cell(row,col).value
                    sheet.write(row-count,col,val)
    while index < 3 :
        if index == 1 :
            sheet1 = w.add_sheet('Info')
        else :
            sheet2 = w.add_sheet('Sheet3')

        s = wb.sheet_by_index(index)
        index =+ 1
        for row in range(s.nrows):
            coln =0
            val = s.cell(row,coln).value
            for col in range(s.ncols):
                val = s.cell(row,col).value
                if index == 1:
                    sheet1.write(row,col,val)
                else :
                    sheet2.write(row,col,val)
w.save(newexcel)
newexcel=“newexcel.xls”
计数=0
wb=xlrd.open_工作簿('/home/sam/myexcel.xls')
w=工作簿()
工作表=w.添加工作表(“输入资源”)
索引=0
s=wb.表按索引(索引)
如果指数<1:
指数=+1
对于范围内的行(s.nrows):
coln=0
val=s.cell(行,列).value
如果val在MissingId中:
mylist.append(val)
计数+=1
其他:
对于范围内的列(s.ncols):
val=s.单元格(行、列).值
工作表写入(行计数、列、值)
当指数<3时:
如果索引==1:
sheet1=w.add_表格('Info')
其他:
表2=w.添加表(“表3”)
s=wb.表按索引(索引)
指数=+1
对于范围内的行(s.nrows):
coln=0
val=s.cell(行,列).value
对于范围内的列(s.ncols):
val=s.单元格(行、列).值
如果索引==1:
表1.写入(行、列、值)
其他:
表2.写入(行、列、值)
w、 保存(新建Excel)

任何帮助或提示都将不胜感激:)

下面是检查重复工作表的一些想法:

import xlwt

wb = xlwt.Workbook()
#Adding 2 sheets
ws1 = wb.add_sheet('One')
ws2 = wb.add_sheet('Two')

sheets = wb._Workbook__worksheets #Getting sheets list
sheet_names = [] #List for shhets names
for sheet in sheets:
     sheet_names.append(
         sheet.get_name() #getting sheet name
         ) 
print sheet_names
print True if u"One" in sheet_names else False #check for our name
wb.save('test.xls')
结果:

$python simple.py 
[u'One', u'Two']
True

你能提供一个指向你使用的myexcel.xls文件的链接吗?看一看,我相信如果你采用这种方法,你可以减少你问题中的代码,甚至可能发现到底出了什么问题。你解决了这个问题吗。如果你有问题,请更新这个问题,否则我会再试一次。