Python Forum
how to read txt file, and write into excel with multiply sheet
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to read txt file, and write into excel with multiply sheet
#1
Hi I have faced one problem I wish to read a text file, and when reaching ===== ending, I wish to write into a new separate sheet.
So for example, If I have a text file like below this:

datetime name department
20220101 James IT
20220101 Test IT
================
datetime name country
20220101 James US
20220101 Test US
================

So the default sheet1 will write like this:
datetime name department
20220101 James IT
20220101 Test IT

sheet2 will show
datetime name country
20220101 James US
20220101 Test US

How am I able to establish this result, the =====

I have a code but this only help me to write in a single sheet, but I wish after ==== it will write into a new sheet
import openpyxl, string
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font

f = open('test.txt', 'r+')  # open text
excel = openpyxl.Workbook()
sheet = excel.worksheets
line = f.readline();  # read text
while line:
    list = line.split()  # convert
    if "=" in line:
        pass
    else:            
        print(list123)
        if list [1] == 'userID':
            sheet[0].append(list )  # write into excel
        elif list [2] == 'name':
            sheet[0].append(list )  # write into excel
        else:  
            sheet[0].append(list )  # write into excel
    
    line = f.readline()  # read next line

excel.save('result.xlsx')
I try like this will occur error
from openpyxl.workbook import Workbook
wb = Workbook()
ws1 = wb.create_sheet("Sheet_A")
ws1.title = "Title_A" 
f = open('test.txt', 'r+')  # open text
excel = openpyxl.Workbook()
sheet = excel.worksheets
line = f.readline();  # read text
count =0
while line:
    list= line.split()  # convert
    if list[1] == 'userID':
        ws1[0].append(list)  # write into excel
    elif list[2] == 'name':
        ws1[0].append(list)  # write into excel
    else:  
        ws1.append(list)  # write into excel
    line = f.readline()
excel.save('test123.xlsx')
Reply


Messages In This Thread
how to read txt file, and write into excel with multiply sheet - by jacklee26 - Jan-01-2023, 03:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete file with read-only permission, but write permission to parent folder cubei 6 22,467 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 451 May-03-2024, 07:23 AM
Last Post: Pedroski55
  What does .flush do? How can I change this to write to the file? Pedroski55 3 451 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Python openyxl not updating Excel file MrBean12 1 534 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 615 Feb-07-2024, 12:24 PM
Last Post: Viento
  Last record in file doesn't write to newline gonksoup 3 607 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 3,144 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  write to csv file problem jacksfrustration 11 1,929 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,881 Nov-09-2023, 10:56 AM
Last Post: mg24
  Search Excel File with a list of values huzzug 4 1,451 Nov-03-2023, 05:35 PM
Last Post: huzzug

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020