python代码,请大家帮忙。
本帖最后由 快乐的小驹 于 2024-1-17 09:53 编辑待处理表格
当前实现效果:还只能生成一行。
已知Bug:输入数量低于17就乱了。
想要的效果
import tkinter as tk
from tkinter import filedialog
import openpyxl
import random
def open_file():
file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx"), ("All Files", "*.*")])
file_path_label.config(text=file_path)
def generate_data():
file_path = file_path_label.cget("text")
if not file_path:
return
wb = openpyxl.load_workbook(file_path)
sheet = wb.active
# 从多行文本框获取要生成的1的数量
num_cells_str = num_cells_entry.get("1.0", "end-1c")
if not num_cells_str.isdigit():
return
num_cells = int(num_cells_str)
# 计算可以插入的空数据数量
num_empty = max(0, 31 - num_cells)
# 遍历第2行,为每个单元格生成一个随机数
for col in range(1, 32):# Assuming the data starts from column A and ends at column Z
# 随机选择一个空数据或1
if random.randint(0, 1) == 0:
# 如果空数据数量大于0,则插入空数据
if num_empty > 0:
sheet.cell(row=2, column=col, value='')
num_empty -= 1
else:
sheet.cell(row=2, column=col, value=1)
else:
sheet.cell(row=2, column=col, value=1)
wb.save(file_path)
root = tk.Tk()
root.title("Excel Generator")
open_file_button = tk.Button(root, text="打开文件", command=open_file)
open_file_button.pack()
file_path_label = tk.Label(root, text="")
file_path_label.pack()
num_cells_label = tk.Label(root, text="要生成的1的数量:")
num_cells_label.pack()
num_cells_entry = tk.Text(root, height=10, width=20)
num_cells_entry.pack()
generate_button = tk.Button(root, text="生成数据", command=generate_data)
generate_button.pack()
root.mainloop()
代码及表格文件:https://veong.lanzouo.com/i3EDE1l03qyb
pandas填充数据很简单 没理解这是要做什么? yibrother 发表于 2024-1-13 19:38
pandas填充数据很简单
这个建议非常可行!用pandas实现,然后存为excel即可。需要修改则pandas读取excel,利用pandas的函数来实现,相当方便,再保存即可! 读取文本那里需要split一下
import tkinter as tk
from tkinter import filedialog
import openpyxl
import random
def open_file():
file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx"), ("All Files", "*.*")])
file_path_label.config(text=file_path)
def generate_data():
file_path = file_path_label.cget("text")
if not file_path:
return
wb = openpyxl.load_workbook(file_path)
sheet = wb.active
# 从多行文本框获取要生成的1的数量
num_cells_strs = num_cells_entry.get("1.0", "end-1c").split('\n')
for i in range(0,len(num_cells_strs)):
num_cells_str=num_cells_strs
irow=2+i
if not num_cells_str.isdigit():
return
num_cells = int(num_cells_str)
# 计算可以插入的空数据数量
num_empty = max(0, 31 - num_cells)
# 遍历第2行,为每个单元格生成一个随机数
for col in range(1, 32):# Assuming the data starts from column A and ends at column Z
# 随机选择一个空数据或1
if random.randint(0, 1) == 0:
# 如果空数据数量大于0,则插入空数据
if num_empty > 0:
sheet.cell(row=irow, column=col, value='')
num_empty -= 1
else:
sheet.cell(row=irow, column=col, value=1)
else:
sheet.cell(row=irow, column=col, value=1)
wb.save(file_path)
root = tk.Tk()
root.title("Excel Generator")
open_file_button = tk.Button(root, text="打开文件", command=open_file)
open_file_button.pack()
file_path_label = tk.Label(root, text="")
file_path_label.pack()
num_cells_label = tk.Label(root, text="要生成的1的数量:")
num_cells_label.pack()
num_cells_entry = tk.Text(root, height=10, width=20)
num_cells_entry.pack()
generate_button = tk.Button(root, text="生成数据", command=generate_data)
generate_button.pack()
root.mainloop()
camgal 发表于 2024-1-13 20:04
读取文本那里需要split一下
import tkinter as tk
这个感觉应该是最贴合楼主的回答,但是可以导入库的话 用pandas 要简单些 我也想要下载一个,可以啊 import tkinter as tk
from tkinter import filedialog, messagebox
import openpyxl
import random
def open_file():
file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx"), ("All Files", "*.*")])
file_path_label.config(text=file_path)
def generate_data():
file_path = file_path_label.cget("text")
if not file_path:
return
wb = openpyxl.load_workbook(file_path)
sheet = wb.active
# 从多行文本框获取要生成的1的数量
num_cells_strs = num_cells_entry.get("1.0", "end-1c").split('\n')
for i in range(0, len(num_cells_strs)):
num_cells_str = num_cells_strs
row = 2 + i
if not num_cells_str.isdigit():
tk.messagebox.showinfo('提示', '因为第{}行不是数字,所以表中第{}行没有更新'.format((row-1), row))
continue
num_cells = int(num_cells_str)
# 遍历每行,为每个单元格生成一个随机数
for col in range(1, 32):
sheet.cell(row=row, column=col, value='')
one_indices = random.sample(range(1, 32), num_cells) # 随机选择一个空数据或1
for col in one_indices:
sheet.cell(row=row, column=col, value=1)
wb.save(file_path)
root = tk.Tk()
root.title("Excel Generator")
open_file_button = tk.Button(root, text="打开文件", command=open_file)
open_file_button.pack()
file_path_label = tk.Label(root, text="")
file_path_label.pack()
num_cells_label = tk.Label(root, text="要生成的1的数量:")
num_cells_label.pack()
num_cells_entry = tk.Text(root, height=10, width=20)
num_cells_entry.pack()
generate_button = tk.Button(root, text="生成数据", command=generate_data)
generate_button.pack()
root.mainloop() losingstars 发表于 2024-1-17 18:33
import tkinter as tk
from tkinter import filedialog, messagebox
import openpyxl
根据楼上的代码改了改。感觉这程序没啥用呀。
页:
[1]