简体   繁体   English

Python 不使用 xlsxwriter 创建 Excel 文件

[英]Python Not Creating Excel File with xlsxwriter

I have an Excel file with items and descriptions and I'm trying to compare descriptions for similarity, and if they're similar, put them in a new Excel file.我有一个包含项目和描述的 Excel 文件,我正在尝试比较描述的相似性,如果它们相似,请将它们放入一个新的 Excel 文件中。 Those items also have Catalog #'s and I'm comparing them to see if they're nothing like each other and they're from the same vendor (buy_line) put them also on the new Excel file.这些项目也有目录编号,我正在比较它们以查看它们是否彼此不同并且它们来自同一个供应商 (buy_line),并将它们也放在新的 Excel 文件中。 When I run the file, it takes way too long and after I leave it running, I come back and Spyder is closed and no new file.当我运行文件时,它花费的时间太长,在我让它运行后,我回来了,Spyder 已关闭,没有新文件。 So this is a 2 part question, is there a way to make the code faster?所以这是一个两部分的问题,有没有办法让代码更快? and why is there no file created?为什么没有创建文件? Thank you in advance.先感谢您。 My code is below我的代码在下面

`import xlrd
import xlsxwriter
from fuzzywuzzy import fuzz

AllItems = xlrd.open_workbook('2-18All_Items-CleanUp.xlsx','rb')
sheets = AllItems.sheet_names()
item = []
base = []
kit = []
buy_line = []
catalogs = []
descriptions = []
similar_desc_item = []
similar_desc = []
diff_catalog_samebuyline = []
sh = AllItems.sheet_by_index(0)

def readexcelfunc():
    for rownum in range(sh.nrows):
        row_values = sh.row_values(rownum)
        item.append((row_values[0]))
        base.append((row_values[1]))
        kit.append((row_values[2]))
        buy_line.append((row_values[6]))
        catalogs.append((row_values[8]))
        descriptions.append((row_values[12]))

def check_similar_desc():
    for i,k in enumerate(descriptions):
        for j,l in enumerate(descriptions):
            ratio1 = fuzz.token_sort_ratio(k,l)
            if ratio1 > 95 and k != l and base[i] != base[j] and kit[i] == "No":
                similar_desc_item.append(item[i])

def check_notmatching_catalog():
    for x,a in enumerate(catalogs):
        for y,b in enumerate(catalogs):
            ratio2 = fuzz.token_sort_ratio(a,b)
            if ratio2 < 10 and buy_line[x] == buy_line[y]:
                diff_catalog_samebuyline.append(catalogs[x])

def Create_ExcelFile():
    NewWorkbook = xlsxwriter.Workbook('Sim_Desc.xlsx')
    worksheet = NewWorkbook.add_worksheet()
    row1 = 0
    row2 = 0
    for items in similar_desc_item:
        worksheet.write(row1,0,items)
        row1 += 1
    for catalognumb in diff_catalog_samebuyline:
        worksheet.write(row2,3,catalognumb)
        NewWorkbook.save()
        NewWorkbook.close()

readexcelfunc()
check_similar_desc()
print (similar_desc_item)
check_notmatching_catalog()
Create_ExcelFile()

print("Finished")`

There are a few issues in the Create_ExcelFile() function. Create_ExcelFile()函数中存在一些问题。 The first is that there is no workbook save() method.首先是没有工作簿save()方法。 Also you aren't incrementing row2, so the second write() will alway write to the first row, and overwrite whatever else is there.此外,您不会增加 row2,因此第二个write()将始终写入第一行,并覆盖那里的任何其他内容。 However, most importantly, the close() method is at the wrong level so you are closing the file too early.但是,最重要的是, close()方法处于错误级别,因此您过早关闭文件。 Something like this should work:这样的事情应该工作:

def Create_ExcelFile():
    NewWorkbook = xlsxwriter.Workbook('Sim_Desc.xlsx')
    worksheet = NewWorkbook.add_worksheet()

    row1 = 0
    row2 = 0

    for items in similar_desc_item:
        worksheet.write(row1,0,items)
        row1 += 1

    for catalognumb in diff_catalog_samebuyline:
        worksheet.write(row2,3,catalognumb)
        # Fix the row2 increment!!

    NewWorkbook.close()

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

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