Python Forum
How do I write all csv records to Excel ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I write all csv records to Excel ?
#1
Dear Pro !!
Can you Help me please with this issue ?

I wrote the following code to copy all csv to exist excel file
but It Copy just the last record and I can't understand why?

def emptyrow():

    # Load the Excel file and select the worksheet
    workbook = openpyxl.load_workbook('test.xlsx')
    worksheet = workbook.active

    # Find the last row with data in the worksheet
    last_row = worksheet.max_row
    print(last_row)

    # Load the CSV file and read the data
    with open('D:\Downloads\A (2).csv', 'r') as csv_file:
        csv_reader = csv.reader(csv_file)
        data = list(csv_reader)
        #print(data)        

    # Write the CSV data to the worksheet
    for row in data:
        for col, value in enumerate(row):
            worksheet.cell(row=last_row+1, column=col+1, value=value)

    # Save the changes to the Excel file
    workbook.save('test.xlsx')

emptyrow()
Please Advise ?
Larz60+ write Mar-23-2023, 11:47 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
Not fully tested, but from a quick look I can see that in the nested for loops (lines 18 to 20) the variable row here for row in data: is overwritten at line 20: worksheet.cell(row=last_row + 1, column=col + 1, value=value) (see the row=... part?).

You're going to want to correct that and see if it fixes your problem.

Sorry: scrub that; I'm reading it wrong.

I've just done a quick test of your code with a simple .csv file:

Output:
10,20,30,40,50
... and all five numbers have been appended to row 4 (which was the first empty row in my .xlsx file) , cols A to E. That said, the numbers have been appended as 'text', rather than integers.

A quick fix for that would be to have worksheet.cell(row=last_row + 1, column=col + 1, value=int(value))

Maybe the issue is with your .csv file?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
This is your problem:
    for row in data:
        for col, value in enumerate(row):
            worksheet.cell(row=last_row+1, column=col+1, value=value)
You set the row to last_row+1 and last_row never changes.

You could do this:
    for row in data:
        last_row += 1
        for col, value in enumerate(row, start=1):
            worksheet.cell(row=last_row, column=col, value=value)
or this:
    for r, row in enumerate(data, start=last_row+1):
        for c, value in enumerate(row, start=1):
            worksheet.cell(row=r, column=c, value=value)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,895 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Openpyxl manipulate excel write formula SamLiu 0 1,049 Nov-04-2022, 03:00 PM
Last Post: SamLiu
  dataframe write to tab delimited differs from Excel koh 0 1,999 Aug-01-2021, 02:46 AM
Last Post: koh
  Read and write active Excel file euras 4 3,481 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  write to excel will be empty in column jacklee26 7 3,354 Jun-27-2020, 12:09 AM
Last Post: snippsat
  Write tables from Word (.docx) to Excel (.xlsx) using xlsxwriter jackie 1 3,198 May-27-2020, 11:47 PM
Last Post: mcmxl22
  Unable to write to excel - Using openpyxl module starstud 2 4,455 Feb-05-2020, 03:53 AM
Last Post: starstud
  I can write to but cannot read from Excel using python. Emerogork 2 2,708 Feb-13-2018, 08:27 AM
Last Post: snippsat
  Script to read, write data to excel file and update it to firebase Expertlearner 1 3,485 Jan-03-2018, 03:32 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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