![]() |
Copy certain cells into new workbook certain cells - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Copy certain cells into new workbook certain cells (/thread-28330.html) |
Copy certain cells into new workbook certain cells - Kristenl2784 - Jul-14-2020 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) RE: Copy certain cells into new workbook certain cells - Yoriz - Jul-14-2020 == does a comparison, use = to assign
RE: Copy certain cells into new workbook certain cells - Kristenl2784 - Jul-14-2020 (Jul-14-2020, 03:51 PM)Yoriz Wrote: I receive the below error now that I changed it from == to = Do you by chance know what this means?
Hello, I figured it out I was missing .value RE: Copy certain cells into new workbook certain cells - Yoriz - Jul-14-2020 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 RE: Copy certain cells into new workbook certain cells - Kristenl2784 - Jul-14-2020 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].valuewould 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'].valueso ws2 incremented by one. |