Excel -Aufgabe mit Python Automation [geschlossen]Python

Python-Programme
Anonymous
 Excel -Aufgabe mit Python Automation [geschlossen]

Post by Anonymous »

Okay, um ehrlich zu sein, bin ich ziemlich festgefahren. Spalten jederzeit, wenn ich den Code ausführe. Ich füge das Bild hinzu, daher ist es einfacher zu verstehen, was ich meine. /> Daten aus der Referenzdatei in die anderen Dateien kopieren. Wenn die Spalten gleich sind. Mein Code:

Code: Select all

import pandas as pd
import tkinter as tk
from tkinter import filedialog

# Function to load the reference file and get column names
def load_reference_file():
ref_file = filedialog.askopenfilename(title="Select Reference Excel File", filetypes=[("Excel Files", "*.xlsx")])
ref_df = pd.read_excel(ref_file)
return ref_df, ref_df.columns.tolist()

# Function to select fields from the reference file and update target files
def update_files(reference_df, fields_to_update):
# Select files to update
target_files = filedialog.askopenfilenames(title="Select Target Excel Files", filetypes=[("Excel Files", "*.xlsx")])

for file in target_files:
target_df = pd.read_excel(file)

# For each field to update, check if the target file contains the field and update it
for field in fields_to_update:
if field in target_df.columns:
target_df[field] = reference_df[field]  # Update the field with the reference data

# Save the updated file
target_df.to_excel(file, index=False)
print(f"Updated {file}")

# GUI to select fields
def select_fields(reference_df):
def on_submit():
fields_to_update = [field for field in fields if field_var.get(field)]
window.destroy()
update_files(reference_df, fields_to_update)

window = tk.Tk()
window.title("Select Fields to Update")

fields = reference_df.columns.tolist()
field_var = tk.BooleanVar()

for field in fields:
tk.Checkbutton(window, text=field, variable=field_var, onvalue=field, offvalue=None).pack()

tk.Button(window, text="Submit", command=on_submit).pack()
window.mainloop()

# Main program
def main():
# Load the reference file
reference_df, columns = load_reference_file()

# Ask user to select which fields to update
select_fields(reference_df)

if __name__ == "__main__":
main()

[1]: https://i.sstatic.net/QsGrI4Kn.png

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post