Python Forum
Copy certain cells into new workbook certain cells
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy certain cells into new workbook certain cells
#1
Hello,

I'm trying to copy certain cells from ws, and paste them into certain cells in ws2, what I have isn't doing this. It's just creating an output file called 'NNM_Comparison.xlsx' which is fine but it has none of the data in it.

import openpyxl as xl; 
import os
 
input_dir = 'C:\\work\\comparison\\NMN'
template = 'C:\\work\\comparison\\template.xlsx'
newFile = 'NNM_Comparison.xlsx'




files = [file for file in os.listdir(input_dir)
         if os.path.isfile(file) and file.endswith(".xlsx")]

for file in files:
    input_file =  os.path.join(input_dir, file)
    wb1=xl.load_workbook(input_file)
    ws=wb1.worksheets[0]
    ws['A1'].value[28:]
    ws['B4'].value
    ws['D4'].value
    ws['B5666'].value
    ws['D5666'].value
	
    wb2 = xl.load_workbook(template) 
    ws2 = wb2.worksheets[0] 
    ws2['A2']==ws['A1']
    ws2['D2']==ws['B4']
    ws2['E2']==ws['D4']
    ws2['I2']==ws['B5666']
    ws2['J2']==ws['D5666']
    
	
    output_file = (newFile)
    wb2.save(output_file)
Reply
#2
== does a comparison, use = to assign
Reply
#3
(Jul-14-2020, 03:51 PM)Yoriz Wrote: == does a comparison, use = to assign

I receive the below error now that I changed it from == to =
Do you by chance know what this means?

Error:
raise ValueError("Cannot convert {0!r} to Excel".format(value)) ValueError: Cannot convert <Cell 'A110-6'.A1> to Excel


Hello,
I figured it out I was missing .value
Reply
#4
you also need to call the value method on the cell you are reading on the right to put it into the cell on the left.
example
ws2['A2']=ws['A1'].value
Reply
#5
How would I go about making ws2 increment by one cell every time it loops to the next file? The cell data for ws1 can remain the same because it's always the same cell data for each file. But ws2 is the template, and I need the cell that the data gets pasted into to increment each time the for loop grabs the next file.

ws2['A2']=ws1['A1].value
would work if I wasn't looping through files, and only had one file to copy and paste from. On the next file I would need it to be
ws2['A3']=ws1[A1'].value
so ws2 incremented by one.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 252 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  How to copy work sheet data one workbook to other? sayyedkamran 2 691 Nov-03-2023, 09:10 AM
Last Post: Larz60+
  xlwings error when reading a workbook Mishal0488 1 1,099 Aug-01-2023, 02:05 AM
Last Post: deanhystad
  getting unexpected indent errors trying to move cells up jensengt 4 866 Jun-28-2023, 12:05 PM
Last Post: deanhystad
  Help in opening and editing an excel workbook from a web-browser test 4 1,434 Aug-10-2022, 02:31 PM
Last Post: test
  Converting cells in excel to JSON format desmondtay 4 1,732 May-23-2022, 10:31 AM
Last Post: Larz60+
  Openpyxl-change value of cells in column based on value that currently occupies cells phillipaj1391 5 9,790 Mar-30-2022, 11:05 PM
Last Post: Pedroski55
  gspread - applying ValueRenderOption to a range of cells dwassner 0 1,694 Jan-12-2022, 03:05 PM
Last Post: dwassner
  Python “Formula” Package: How do I parse Excel formula with a range of cells? JaneTan 1 2,676 Jul-12-2021, 11:09 AM
Last Post: jefsummers
  Add a new column when I extract each sheet in an Excel workbook as a new csv file shantanu97 0 2,228 Mar-24-2021, 04:56 AM
Last Post: shantanu97

Forum Jump:

User Panel Messages

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