Home > other >  Using xlutils. Copy. The copy prompt list index out of range
Using xlutils. Copy. The copy prompt list index out of range

Time:01-23

1. The use of development tools for Visual Studio 2019, Python version 3.7, the expansion pack version below

2. There is a demand on my side, in the use of xlrd2 read an Excel spreadsheet, then use the xlutils. Copy. The copy edit the Excel function, implement the data updated to save the Excel spreadsheet, tabular XLS format, the problem is when I was in the first time to carry out the method, running perfectly normal can get what I want results, but the second time when carrying the prompt error "list index out of range"

3. I try to modify the bk bk1, xlrd2 I to import again as xlrd22, then use xlrd22. Open_workbook tried, but I found that has nothing to do with bk object inspection parameters, I noticed in the programming software bk has full access to the form data, so I think the problem on the copy method, but simply do not know how to modify to achieve my purpose, no idea,,, excel size is 2445 KB, a total of 12 sheet, strives for the bosses to give directions,

Attach two method code:
# statement amount perspective
Def secondStepII () :
Bk=xlrd2. Open_workbook (" data \ \ "+ fileName +" secondary. XLS ", formatting_info=True) # open file
Wt=xlutils. Copy. The copy (bk) # copy
Sh=defending sheet_by_index (# 1) using WLRD read sheet2
Nrows=sh. Nrows # access lines
Ncols=sh. Ncols # for the number of columns
Sheet=wt. Get_sheet (7) # WLWT read sheet8
# to write the content cell
# sheet. Write (0, 0, "Test")
ErjiList=[]
For I in range (1, nrows) :
ErjiList. Append (sh) cell_value (I, 10))
Dict={}
For the key in erjiList:
Dict [key]=dict. Get (key, 0) + 1
Aa=2;
Bb=0;
AllCount=0;
Sheet. Write (1, 0, "name" 2)
Sheet. Write (1, 1, "count")
For I in dict:
Sheet. Write (aa, bb, I)
Bb +=1
Sheet. Write (aa, bb, dict [I])
AllCount +=dict [I]
Bb -=1
Aa +=1
Aa +=1
Sheet. Write (aa, bb, "total")
Bb +=1
Sheet. Write (aa, bb, allCount)
Wt. Save (" data \ \ "+ fileName +" secondary. XLS ") # save


# backlog perspective
Def thirdStepII () :
Bk=xlrd2. Open_workbook (" data \ \ "+ fileName +" secondary. XLS ", formatting_info=True) # open file
Wt=xlutils. Copy. The copy (bk) # copy PS: come here to start the error list index out of range
Sh=defending sheet_by_index # (2) using WLRD read sheet3
Nrows=sh. Nrows # access lines
Ncols=sh. Ncols # for the number of columns
Sheet=wt. Get_sheet (8) # WLWT read sheet9
# to write the content cell
# sheet. Write (0, 0, "Test")
ErjiList=[]
For I in range (1, nrows) :
ErjiList. Append (sh) cell_value (I, 10))
Dict={}
For the key in erjiList:
Dict [key]=dict. Get (key, 0) + 1
Aa=2;
Bb=0;
AllCount=0;
Sheet. Write (1, 0, "name" 2)
Sheet. Write (1, 1, "count")
For I in dict:
Sheet. Write (aa, bb, I)
Bb +=1
Sheet. Write (aa, bb, dict [I])
AllCount +=dict [I]
Bb -=1
Aa +=1
Aa +=1
Sheet. Write (aa, bb, "total")
Bb +=1
Sheet. Write (aa, bb, allCount)
Wt. Save (" data \ \ "+ fileName +" secondary. XLS ") #

CodePudding user response:

I test to a situation, I put the order of the two methods to perform a transfer, secondStepII executed thirdStepII again first, then an error is on the secondStepII, content of the error is still out range, so I wonder whether it is because the copy method, can only save page 9 sheet, more than nine will go wrong? Have bosses know what to do to solve this problem is not an error or yao,,,

CodePudding user response:

Also seems to make sense, however, the excel spreadsheet is inherently has ten several sheet, the first pass method when the copy is certainly 10 a few list, could not perform it again but will not be able to save more than a dozen, think impassability,,,

CodePudding user response:

Problem has been solved at present, I put the two methods are combined to a piece, after copy to come over, directly modify all the sheet, and then save, without any problem, after the completion of the speculation is copy, copy again, no release, or stored in the xlutils memory, but I looked at the official API didn't find where there is empty, don't know if my speculation is correct, someone to discuss it,,, beginners Python 3 days, met a lot of problems to solve, have bosses know where more active a bit,,,

CodePudding user response:

Yes, you use the two functions, each function has the same excel open - edit - save, so it is out of question,
1, after open - edit - save for the first time, best to release the memory, such as defending release_resources ()
2, if not released, so you the second function, this time to open this file will be an error list index out of range, actually this file is already open, this time can no longer open
In conclusion:
1, two functions of words, the first open after the release, the second function to open the release
2, put the two together again, is actually a function, it is open for only once, you run is completed, the parser is automatically release the memory

My blog: https://blog.csdn.net/NoamaNelson
  • Related