简体   繁体   English

XLSXWriter拒绝创建第二个Excel文件

[英]XLSXWriter refuses to create a second Excel File

I'm working on a program to split excel files into sections of 1000. I can't seem to get it to create a second excel file, as xlsxwriter doesn't create the second file. 我正在开发一个程序,将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!")

I've been rubber ducking this and I can't find where I'm at fault. 我一直在躲避这个,我无法找到我的错。

EDIT: 编辑:

SOLVED!! 解决了!!

By creating a sheet and then closing it, the program can then grasp it. 通过创建工作表然后关闭它,程序就可以掌握它。 I will probably make a git issue about this. 我可能会对此发表一个git问题。

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

Closing it lets open see it while it still contains the same info. 关闭它让我们打开它看到它仍然包含相同的信息。

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

Doesn't the save/close line need to be within the while loop? save/close行不需要在while循环中吗? Otherwise it looks like it will only save either the first/last item: 否则看起来它只会保存第一个/最后一个项目:

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

that line is probably the reason why you cannot read back your file and your script crashes: 该行可能是您无法读回文件和脚本崩溃的原因:

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

'__file__' shouldn't have quotes. '__file__'不应该有引号。 I'd do: 我会做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM