r/StackoverReddit • u/Grouchy_Offer_2763 • Jul 03 '24
Python Help Creating Code That Goes Through Excel File and Selectively Copies Data Into Different Excel File
Hi all!
This is a repeat post, but I think posting it in this subreddit is more appropriate than that which I originally posted in as it is a pretty specific request. I am trying to create a code that will iterate through rows in an Excel file. Starting with column E and up through column Z, I would like the program to see if the cell is empty or not. If not, I would like the code to distribute the values of columns A-D in that row into columns A-D of the destination workbook. I would like the current cell (somewhere in columns E-Z) to be placed into Column E of the destination workbook. For context, columns E-Z will contain values that have been separated by Text to Column, but I still want each to be individually associated with the data in columns A-D.
I have included my code below. I am not receiving any error messages but nothing is being distributed into the destination workbook. I would really love any insight into what fixes I can implement or whether this is close to anything (should I approach it differently, such as using pandas?). Thank you very much in advance!!
import openpyxl
from openpyxl import Workbook, load_workbook
def copy_data(source_sheet, destination_sheet):
for row in source_sheet.iter_rows(min_row=2, min_col=1, max_col=26, values_only=True):
for cell in row:
if cell is not None:
# Determine the column index of the current cell
column_index = row.index(cell) + 1 # Index starts from 0, column from 1
# Copy data from source to destination sheet
dest_row = destination_sheet.max_row + 1
destination_sheet.cell(row=dest_row, column=1).value = source_sheet.cell(row=column_index, column=1).value # Column A
destination_sheet.cell(row=dest_row, column=2).value = source_sheet.cell(row=column_index, column=2).value # Column B
destination_sheet.cell(row=dest_row, column=3).value = source_sheet.cell(row=column_index, column=3).value # Column C
destination_sheet.cell(row=dest_row, column=4).value = source_sheet.cell(row=column_index, column=4).value # Column D
destination_sheet.cell(row=dest_row, column=5).value = str(column_index) # Current cell in columns E-Z
# Load the source and destination workbooks
source_wb = load_workbook('/Users/KALII/Downloads/Data For Delimiter Experiment.xlsx')
destination_wb = load_workbook('/Users/KALII/Downloads/SeniorManagementTrackerAppendRows.xlsx')
# Assigning sheets
source_sheet = source_wb.worksheets[0]
destination_sheet = destination_wb.active
copy_data(source_sheet, destination_sheet)
# Save the destination workbook
destination_wb.save('SeniorManagementTrackerAppendRows.xlsx')
2
u/TaeTaeDS Jul 03 '24
This is a job better done with SQL. You could use Python to connect.
2
u/jrandom_42 Jul 04 '24
OP, can you explain why this must be done directly to Excel files?
Personally I would prefer to implement this by working with CSVs exported from Excel or, as u/TaeTaeDS suggests, putting the data in an actual database. The requirement to do it all inside Excel might be an XY problem.
If it's unavoidably necessary to manipulate the Excel files directly, because you have to work with constraints that someone else is imposing on you, then I'd suggest implementing your solution in VBA, ie, use the native tool that Office supports. You'll encounter the least possible friction that way. Automating Office with anything other than VBA has been a tricky proposition for as long as I can remember.
1
u/RobbinYoHood Jul 03 '24
Try debugging:
1
u/Centimane Jul 04 '24
As an aside, just about every number in your code should be using a parameter so you can adjust its behavior without updating the code. Same with the paths.
1
u/chrisrko Moderator Aug 08 '24
INFO!!! We are moving to r/stackoverflow !!!!
We want everybody to please be aware that all future posts and updates from us will from now on be on r/stackoverflow
We made an appeal to gain ownershift of r/stackoverflow because it has been abandoned, and it got granted!!
So please migrate with us to our new subreddit r/stackoverflow ;)