Modify an existing Excel file using Openpyxl in Python

0 votes

I am basically trying to copy some specific columns from a CSV file and paste those in an existing excel file[*.xlsx] using python. Say for example, you have a CSV file like this :

 col_1   col_2   col_3  col_4
  1        2       3     4
  5        6       7     8
  9       10      11    12 

So, i wanted to copy the both col_3 and col_4 and paste those in col_8 and col_9 in an existing excel file [which is a .XLSX format]. I have tried this in various way to solve, but could not find out the exact way. i tried something like this :

with open( read_x_csv, 'rb') as f:
    reader = csv.reader(f)
    for row in reader: 
            list1 = row[13] 
            queue1.append(list1)
            list2 = row[14] 
            queue2.append(list2)
            list3 = row[15] 
            queue3.append(list3)
            list4 = row[16] 
            queue4.append(list4)

and then

 rb = open_workbook("Exact file path.....")
 wb = copy(rb)
 ws = wb.get_sheet(0) 

 row_no = 0

 for item in queue1:
    if(item != ""):
            ii = int(item)
            ws.write(row_no,12,ii) 
            row_no = row_no + 1
            #ws.write(item)
            print item
    else:

            ws.write(row_no,12,item) 
            row_no = row_no + 1

  wb.save("Output.xls") 

but problem with this solution is it does not allow me to save as *.XLSX format which is strictly required for me.

I have tried to use Openpyxl as it can handle *.XLSX format, but could not find out a way to modify the existing excel file. can anyone please help on this?

Doubt : 1) Can we really read a whole column from a CSV file and store into an array/list using python? 2) Can we modify the existing excel file which is in .XLSX format using openpyxl or any other package?

Mar 25, 2022 in Database by Edureka
• 13,670 points
1,921 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Database

0 votes
1 answer

Reading an Excel file in python using pandas

How to Use Pandas to Import an ...READ MORE

answered Mar 30, 2022 in Database by gaurav
• 23,260 points
2,412 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Save dynamically created table content in excel file using SheetJS

text inside tr instead of td in dynamic content. This results ...READ MORE

answered Apr 4, 2022 in Database by Edureka
• 13,670 points
3,316 views
0 votes
1 answer

How to download created excel file in node.js using exceljs

Download a sample Excel file using Node. ...READ MORE

answered Apr 4, 2022 in Database by Edureka
• 13,670 points
7,283 views
0 votes
1 answer

How to create an excel file in android?

In Android, create Excel XLSX or XLS ...READ MORE

answered Apr 4, 2022 in Database by Edureka
• 13,670 points
1,079 views
0 votes
1 answer

How to open a password protected excel file using python?

I recently came upon a Python package ...READ MORE

answered Apr 6, 2022 in Database by gaurav
• 23,260 points
9,263 views
0 votes
1 answer

Modify schema in HBase using Programs

HBase schemas can be created or updated ...READ MORE

answered Aug 9, 2018 in Database by kurt_cobain
• 9,390 points
894 views
0 votes
1 answer

How to generate the statement CREATE TABLE for an existing table in postgreSQL?

Generate table creation statement for an existing ...READ MORE

answered Feb 9, 2022 in Database by Neha
• 9,060 points
6,156 views
0 votes
1 answer

Date difference: different results in Excel vs. Python

To calculate the date difference, we can ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
952 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP