Recently in order to realize some personal ideas need to use the data obtained in Python to Excel, referring to some of the online writing tutorials. Finally, the write format that satisfies the inner needs is completed, and the writing method is shared.
First, install the OPENPYXL library for Python, which allows Python to read and write to the. Xlxs Table File
The library is typically installed using PIP install OPENPYXL
This method is mainly for a large number of data to be obtained through a For loop write method, not much to say, the following is the code case:
fromOpenpyxlImportworkbooklist=["ASDASF|DASFSAFDA","SDWEEWEEW|GDSDADASD","Fghkjl|werqerwae"]WB=Workbook () sheet=wb.active forI, lineinchEnumerate (list):Print(i + 1, line) a= Line.split ('|') forJ,kinchEnumerate (a):Print(j+1, K) sheet["a%d"% (i + 1)].value =k Wb.save ('newe.xlsx')
This method enables the "|" Data is stored in NEWE.XLSX as a column
Here is a case where all the string contents need to be stored:
fromOpenpyxlImportworkbooklist=["ASDASF|DASFSAFDA","SDWEEWEEW|GDSDADASD","Fghkjl|werqerwae"]WB=Workbook () sheet=wb.active forI, lineinchEnumerate (list):Print(i + 1, line) a= Line.split ('|') sheet["a%d"% (i + 1)].value =A[0] sheet["b%d"% (i + 1)].value = a[1] Wb.save ('newe.xlsx')
The above is simple to share, hope to be useful to you
About Excel writing cases in Python