吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 540|回复: 7
收起左侧

[已解决] python代码~实在是弄不出来了~求助

 关闭 [复制链接]
快乐的小驹 发表于 2024-1-12 15:32
本帖最后由 快乐的小驹 于 2024-1-13 18:24 编辑

待处理表格一
1.png


保存到表格二
2.png


目前效果
3.png


想要的效果
{AEDDB40E-102B-4ac1-8872-A810E573D2F0}.png

代码:
[Python] 纯文本查看 复制代码
import openpyxl
from tkinter import Tk, Button, Text, filedialog, messagebox

def open_file1():
    file_path1 = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx"), ("All Files", "*.*")])
    file_path1_label.delete(1.0, "end")
    file_path1_label.insert(1.0, file_path1)

def open_file2():
    file_path2 = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx"), ("All Files", "*.*")])
    file_path2_label.delete(1.0, "end")
    file_path2_label.insert(1.0, file_path2)

def save_file():
    file1 = openpyxl.load_workbook(file_path1_label.get(1.0, "end-1c"))
    sheet1 = file1.active
    file2 = openpyxl.load_workbook(file_path2_label.get(1.0, "end-1c"))
    sheet2 = file2.active

    # 获取文件1的行数
    num_rows = sheet1.max_row

    # 从第二行开始遍历文件1中的每一行
    for row in range(2, num_rows + 1):
        # 获取文件1当前行的所有数据
        row_data = []
        for col in range(1, sheet1.max_column + 1):
            cell = sheet1.cell(row=row, column=col)
            if cell.value is not None:
                row_data.append(cell.value)
            else:
                row_data.append(" ")
        
        # 将文件1当前行的数据转换成字符串A和字符串B
        row_data_a = ['A' + str(x) for x in row_data]
        row_data_b = ['B' + str(x) for x in row_data]
        
        # 将文件1当前行的数据复制成两行写入文件2
        sheet2.append(row_data_a)
        sheet2.append(row_data_b)

    # 在文件2的最后一行添加合并后的数据
        row = sheet2.max_row + 1

    # 保存文件2
        file2.save(file_path2_label.get(1.0, "end-1c"))

    # 提示保存成功
    messagebox.showinfo("保存成功", "文件2已成功保存!")


root = Tk()

file_path1_label = Text(root, height=1, width=50)
file_path1_label.pack()
Button(root, text="打开文件1", command=open_file1).pack()

file_path2_label = Text(root, height=1, width=50)
file_path2_label.pack()
Button(root, text="打开文件2", command=open_file2).pack()

Button(root, text="保存文件2", command=save_file).pack()

root.mainloop()



代码及表格文件:https://veong.lanzouo.com/ihi4P1kweeif

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

~零度 发表于 2024-1-12 17:50
[Python] 纯文本查看 复制代码
import openpyxl
from tkinter import Tk, Button, Text, filedialog, messagebox


def open_file1():
    file_path1 = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx"), ("All Files", "*.*")])
    file_path1_label.delete(1.0, "end")
    file_path1_label.insert(1.0, file_path1)


def open_file2():
    file_path2 = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx"), ("All Files", "*.*")])
    file_path2_label.delete(1.0, "end")
    file_path2_label.insert(1.0, file_path2)


def save_file():
    file1 = openpyxl.load_workbook(file_path1_label.get(1.0, "end-1c"))
    sheet1 = file1.active
    file2 = openpyxl.load_workbook(file_path2_label.get(1.0, "end-1c"))
    sheet2 = file2.active

    # 获取文件1的行数
    num_rows = sheet1.max_row

    # 从第二行开始遍历文件1中的每一行
    for row in range(2, num_rows + 1):
        # 获取文件1当前行的所有数据
        row_data_a = []
        row_data_b = []
        for col in range(1, sheet1.max_column + 1):
            cell = sheet1.cell(row=row, column=col)
            if cell.value is not None:
                # 要加数据的话,就改成注释那样子
                row_data_a.append('A')  # row_data_a.append('A' + str(cell.value))
                row_data_b.append('B')  # row_data_a.append('B' + str(cell.value))
            else:
                row_data_a.append(None)
                row_data_b.append(None)

        # 将文件1当前行的数据复制成两行写入文件2
        sheet2.append(row_data_a)
        sheet2.append(row_data_b)

        # 在文件2的最后一行添加合并后的数据
        row = sheet2.max_row + 1

        # 保存文件2
        file2.save(file_path2_label.get(1.0, "end-1c"))

    # 提示保存成功
    messagebox.showinfo("保存成功", "文件2已成功保存!")


root = Tk()

file_path1_label = Text(root, height=1, width=50)
file_path1_label.pack()
Button(root, text="打开文件1", command=open_file1).pack()

file_path2_label = Text(root, height=1, width=50)
file_path2_label.pack()
Button(root, text="打开文件2", command=open_file2).pack()

Button(root, text="保存文件2", command=save_file).pack()

root.mainloop()

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
快乐的小驹 + 1 + 1 谢谢@Thanks!

查看全部评分

testOrdev123 发表于 2024-1-12 17:22
遍历的时候读到1就替换为字母,偶数行换A,奇数行换B,代码有问题就debug慢慢看
angelb 发表于 2024-1-12 17:30
你应该没有写全  很简单 你这个实现,先放到一个列表进行处理
天下客 发表于 2024-1-12 17:45
本帖最后由 天下客 于 2024-1-12 17:48 编辑

你把
[Python] 纯文本查看 复制代码
        # 将文件1当前行的数据转换成字符串A和字符串B
        row_data_a = ['A' + str(x) for x in row_data]
        row_data_b = ['B' + str(x) for x in row_data]

修改为
        
[Python] 纯文本查看 复制代码
# 将文件1当前行的数据转换成字符串A和字符串B
        row_data_a = ['A' for x in row_data if x is not " "]
        row_data_b = ['B' for x in row_data if x is not " "]

我没有试,你自己试下
HonDmOon 发表于 2024-1-12 18:25
本帖最后由 HonDmOon 于 2024-1-12 18:32 编辑



思路错了
sai609 发表于 2024-1-13 10:45
ctrll+f,替换
zishen 发表于 2024-1-13 16:10
直接替换呀
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-25 13:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表