覆盖Excel文件不断发生; XLSX;蟒蛇

时间:2017-03-15 07:43:42

标签: excel python-2.7 xlsx overwrite

我尝试过搜索,但找不到与我正在使用的worksheet.xslx类似的内容。我的程序中没有熊猫模块。

目标:例如有5个文件。从文件1中提取一些值并放入第1列。从文件2中提取一些值并放入第2列等。

问题:我使用的代码如下所示。我的程序运行的最新文件将继续覆盖工作表,并删除早期文件。 (即,最后的excel文件仅显示第5列。第1-4列为空。) 请告诉我应该做什么。谢谢!

代码:

import csv
import os
import py2exe
from os import rename, listdir
from xlrd import open_workbook
from xlwt import Workbook
import xlrd
import xlwt
import py2exe
import xlsxwriter
from os.path import basename


filesnum=()
wb=Workbook()

class Window:

 def process_csv(self):
    global index
    global abc
    abc = 0

    direct=self.filename2 #assuming directory is correct

    filesname = "C:\file1" "C:\file2", "etc etc")): #ignore the errors made here as my original codes is correct for this part
    for b in range (len(filesname)):
        abc+=1
        if "123456" in open(filesnum[b]).read():
            wantedarray=list()
            count = 0
            workbook = xlsxwriter.Workbook(direct + "/newbie.xlsx")
            worksheet=workbook.add_worksheet("newbie")

            for line in copyid:
                if "abcde" in line: 
                    wantedno=line[:8]
                    arrayfound.append(wantedno) #take out the number
                elif "ending" in line:
                    break  
                else:
                    continue        
            for index, value in enumerate(arrayfound):
                worksheet.write(index+2, abc, value)
            workbook.close()
        else:
            continue

root = Tk()
window=Window(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

我已下载并使用openpyxl来解决此问题。

wb.save(direct + "/newbie.xlsx")