为什么不打开这个xlsx文件?

我试图使用openpyxl模块来获取电子表格,查看某个列(在本例中为E列)中是否有空单元格,然后将包含这些空单元格的行复制到新的电子表格中。 代码运行时没有回溯,但生成的文件不会打开。 这是怎么回事?

这是我的代码:

#import the openpyxl module import openpyxl #First create a new workbook & sheet newwb = openpyxl.Workbook() newwb.save('TESTINGTHISTHING.xlsx') newsheet = newwb.get_sheet_by_name('Sheet') #open the original file wb = openpyxl.load_workbook('OriginalWorkbook.xlsx') #create a sheet object sheet = wb.get_sheet_by_name('Sheet1') #Find out how many cells of a certain column are left blank, #and what rows they're in count = 0 listofrows = [] for row in range(2, sheet.get_highest_row() + 1): company = sheet['E' + str(row)].value if company == None: listofrows.append(row) count += 1 print listofrows print count #Put the values of the rows with blank company names into the new sheet for i in range(len(listofrows)): j = 0 newsheet['A' + str(i+1)] = sheet['A' + str(listofrows[j])].value j += 1 newwb.save('TESTINGTHISTHING.xlsx') 

请帮忙!

我只是用模拟文档运行你的程序。 我能够打开我的输出文件没有问题。 你的问题可能依赖于你的excel或openpyxl版本。

请提供您的软件版本,除了您的源文件,所以我可以进一步了解这个问题。

您可以随时更新openpyxl:

 c:\Python27\Scripts pip install openpyxl --upgrade