sydomain

思绪来的快,去的也快,偶尔在这里停留。

python批量提取excel中指定的行标跟列标

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

def process_files():
    folder_path = filedialog.askdirectory()
    if folder_path:
        for file_name in os.listdir(folder_path):
            if file_name.endswith(".xlsx") or file_name.endswith(".xls") or file_name.endswith(".csv"):
                file_path = os.path.join(folder_path, file_name)
                if file_name.endswith(".csv"):
                    df = pd.read_csv(file_path)
                else:
                    df = pd.read_excel(file_path)
                selected_rows = df.iloc[[0, 1, 2]]  # 选择行标为0, 1, 2的行
                selected_columns = selected_rows.iloc[:, [0, 1, 2]]  # 选择列标为0, 1, 2的列
                new_file_name = f"new_{file_name}"
                new_file_path = os.path.join(folder_path, new_file_name)
                if file_name.endswith(".csv"):
                    selected_columns.to_csv(new_file_path, index=False)
                else:
                    selected_columns.to_excel(new_file_path, index=False)
                print(f"已处理 {new_file_path}")

root = tk.Tk()
root.title("Excel 指定行列")

# 设置窗口大小和位置
window_width = 300
window_height = 150
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x}+{y}")

# 设置窗口背景颜色
root.configure(bg="#f2f2f2")

# 创建标签部件
label = tk.Label(root, text="选择要处理的文件夹", font=("Arial", 14), bg="#f2f2f2")
label.pack(pady=10)

# 创建按钮部件
button = tk.Button(root, text="选择文件夹", font=("Arial", 12), command=process_files)
button.pack(pady=10)

root.mainloop()

如果是要选择所有的行标 ,那就可以这样写

selected_rows = df.iloc[:]


Powered By sydomain

Copyright Your WebSite.Some Rights Reserved.