XLSXWriter拒绝创建第二个Excel文件

时间:2017-06-21 20:19:35

标签: python xlrd xlsxwriter

我正在开发一个程序,将excel文件拆分为1000个部分。我似乎无法创建第二个excel文件,因为xlsxwriter不会创建第二个文件。

from os.path import join, dirname, abspath
from xlrd.sheet import ctype_text
import csv
import os
import sys
import xlrd
import xlsxwriter
import xlwt

file_paths = sys.argv[1:]
draganddrop = ''.join(file_paths)
beginGrab = 0
counting = 0
endGrab = 1000    
thousands = 0

if draganddrop == "":
    fileName = raw_input("\nInput the file with extension\n>")      
else:
    fileName = draganddrop

stopPoint = fileName.index('.')
prepRev = fileName[stopPoint:]
preName = fileName[:stopPoint]

if prepRev == ".csv":
    excelFile = xlsxwriter.Workbook(preName + '.xlsx')
    worksheet = excelFile.add_worksheet()
    with open(fileName,'rb') as f:
        content = csv.reader(f)
        for index_col, data_in_col in enumerate(content):
            for index_row, data_in_cell in enumerate(data_in_col):
                worksheet.write(index_col,index_row,data_in_cell)
    excelFile.close()
    fileName = (preName + '.xlsx')
    delMe = 1
    print("Temporary Convert to xlsx done.\n")

stopPoint = fileName.index('.')
prepRev = fileName[0:stopPoint]   
fname = join(dirname(abspath(__file__)), fileName)
xl_workbook = xlrd.open_workbook(fname)
sheet_names = xl_workbook.sheet_names()
xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])
book = xlwt.Workbook(encoding="utf-8")
worksheet = book.add_sheet("Results", cell_overwrite_ok=True)

workbook = xlrd.open_workbook(fileName)
for sheet in workbook.sheets():
    for row in range(sheet.nrows):
        row = int(row)

if(int(row)>1000):
    subDivide = int(row) / 1000
    while(thousands != subDivide + 1):
        thousands = thousands + 1  
        counting = 0
        totalName = preName + "_" + str(thousands) + ".xlsx"
        print(totalName)
        excelFile = xlsxwriter.Workbook(str(totalName))
        worksheet = excelFile.add_worksheet()
        with open(totalName,'rb') as f:
            col = xl_sheet.col_slice(0,1,10101010)
            for idx, cell_obj in enumerate(col, start=beginGrab):
                counting = counting + 1
                if(counting == 1000):
                    break                   
                cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')  
                cell_obj_str = str(cell_obj)
                telePhone = (cell_obj_str[7:19])
                worksheet.write(idx+1, 0, "1" + telePhone)          
        worksheet.write(0,0, "Telephone Number")
        beginGrab = thousands * 1000
        endGrab = beginGrab + 1000
        excelFile.close()
        excelFile = None

else:
    print("Mate, this is Tiny!")
print ("Ding! Job Done!")

我一直在躲避这一点,我无法找到我的错。

编辑:

解决!!

通过创建工作表然后关闭它,程序可以掌握它。我可能会对此发表一个git问题。

if prepRev == ".csv":
    totalName = preName + '.xlsx'
    excelFile = xlsxwriter.Workbook(totalName)
    excelFile.close()

关闭它让我们在它仍然包含相同信息的同时打开它。

    excelFile = xlsxwriter.Workbook(totalName)
    worksheet = excelFile.add_worksheet()
    with open(fileName,'rb') as f:

2 个答案:

答案 0 :(得分:2)

save/close行不需要在while循环中吗?否则看起来它只会保存第一个/最后一个项目:

while(thousands != subDivide + 1):
    # write file
    excelFile.close() 

答案 1 :(得分:1)

该行可能是您无法回读文件并导致脚本崩溃的原因:

fname = join(dirname(abspath('__file__')), '%s' % fileName)

'__file__'不应该有引号。我会这样做:

fname = join(dirname(abspath(__file__)), fileName)