python代码~实在是弄不出来了~求助
本帖最后由 快乐的小驹 于 2024-1-13 18:24 编辑待处理表格一
保存到表格二
目前效果
想要的效果
代码:
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 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就替换为字母,偶数行换A,奇数行换B,代码有问题就debug慢慢看 你应该没有写全很简单 你这个实现,先放到一个列表进行处理 本帖最后由 天下客 于 2024-1-12 17:48 编辑
你把
# 将文件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当前行的数据转换成字符串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:32 编辑
{:17_1089:}
思路错了 ctrll+f,替换 直接替换呀
页:
[1]