Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy columns from workbook, paste in second sheet of second workbook, openPyXL

I'm new to openpyxl and am developing a tool that requires copying & pasting columns.

I have a folder containing two sets of excel files. I need the script to iterate through the files, find the ones that are named "GenLU_xx" (xx represents name of a place such as Calgary) and copy Columns C & E (3 & 5). It then needs to find the corresponding file which is named as "LU_Summary_xx" (xx again represents name of place such as Calgary) and paste the copied columns to the second sheet of that workbook. It needs to match GenLU_Calgary with LUZ_Summary_Calgary and so forth for all the files. So far I have not been able to figure out code for copying and pasting columns and the seemingly double iteration is confusing me. My python skills are beginner although I'm usually able to figure out code by looking at examples. In this case I'm having some trouble locating sample code. Just started using openpyxl. I have completed the script except for the parts pertaining to excel. Hopefully someone can help out. Any help would be much appreciated!

EDIT: New to StackOverflow as well so not sure why I got -2. Maybe due to lack of any code?

Here is what I have so far:

    import os, openpyxl, glob
from openpyxl import Workbook
Tables = r"path"
os.chdir(Tables)
for file in glob.glob ("LUZ*"):
    wb = openpyxl.load_workbook(file)
    ws = wb.active
    ws ["G1"] = "GEN_LU_ZN"
    wb.create_sheet(title="Sheet2")
    wb.save(file)

This just adds a value to G1 of every file starting with LUZ and creates a second sheet.

As I mentioned previously, I have yet to even figure out the code for copying the values of an entire column.

I am thinking I could iterate through all files starting with "GenLU*" using glob and then store the values of Columns 3 & 5 but I'm still having trouble figuring out how to access values for columns. I do not have a range of rows as each workbook will have a different number of rows for the two columns.

EDIT 2: I am able to access cell values for a particular column using this code:

for file in glob.glob ("GenLU_Airdrie*"):
    wb = openpyxl.load_workbook(file, use_iterators=True)
    ws = wb.active
    for row in ws.iter_rows ('C1:C200'):
        for cell in row:
            values = cell.value
            print values

However I'm not sure how I would go about 'pasting' these values in column A of the other sheet.

like image 583
gistech007 Avatar asked Feb 09 '23 05:02

gistech007


2 Answers

Charlie's code worked for me by changing 'col=4' to 'column=4' using openpyxl-2.3.3

ws.cell(row=idx, column=4).value = cell.value
like image 147
Limi Avatar answered Feb 12 '23 01:02

Limi


If you really do want to work with columns then you can use the .columns property when reading files.

To copy the values from one sheet to another you just assign them. The following will copy the value of A1 from one worksheet to another.

ws1 = wb1.active
ws2 = wb2.active
ws2['A1'] = ws1['A1'].value

To copy column D code could look something like this

col_d = ws1.columns[3] # 0-indexing
for idx, cell in enumerate(col_d, 1):
    ws.cell(row=idx, col=4).value = cell.value #1-indexing
like image 39
Charlie Clark Avatar answered Feb 11 '23 23:02

Charlie Clark