SoFunction
Updated on 2024-10-29

Python read txt content written in xls format excel method

Since xlwt currently only supports xls format, as for xlsx format, will continue to update later!

import xlwt
import codecs

def Txt_to_Excel(inputTxt,sheetName,start_row,start_col,outputExcel):
 fr = (inputTxt,'r')
 wb = (encoding = 'utf-8')
 ws = wb.add_sheet(sheetName)

 line_number = 0# how many rows the record has, equivalent to i when written to excel.
 row_excel = start_row
 try:
  for line in fr :
   line_number +=1
   row_excel +=1
   line = ()
   line = (' ')
   len_line = len(line)How many numbers are in each row of the #list, equivalent to writing to excel j
   col_excel = start_col
   for j in range(len_line):
    print (line[j])
    (row_excel,col_excel,line[j])
    col_excel +=1
    (outputExcel)
 except:
  print ('')


if __name__=='__main__':
 sheetName = 'Sheet2'# need to be written to Sheet2 in excel, you can set your own
 start_row = 7 # Starting on line 7
 start_col = 3 # Write from column 3
 inputfile = '' # Input files
 outputExcel = 'excel_result.xls' # Output excel file
 Txt_to_Excel(inputfile,sheetName,start_row,start_col,outputExcel)el)

Above this Python read txt content written in xls format excel is all I have to share with you, I hope to be able to give you a reference, and I hope you support me more.