gpt4 book ai didi

python - 将用户输入的数据从 Excel 工作表传输到 Python 脚本

转载 作者:行者123 更新时间:2023-12-04 21:05:03 24 4
gpt4 key购买 nike

我正在编写一个脚本,我希望能够与 MS Excel 进行交互。运行脚本后,我想打开一个 excel 工作表,让用户输入一些数据,在 excel 中进行一些基本计算,然后将计算的数据和一些用户输入的数据返回给脚本。

我这样做是因为我想允许用户一次输入所有数据,而不必在提示后回答提示,还因为我是 Python 新手,甚至无法编写 GUI。

这是我正在尝试做的一个例子:

表 1(Excel 工作表中的 range1):

用户输入分数和 xa:aa 数据,excel 计算混合。 xa:aa 上的混合信息返回到我的 python 脚本中,在我的脚本中用于进一步计算。数据输入表实际上更长(更多的数据输入行),但只是显示了足够的子集来让我对我正在尝试做的事情有所了解:

Stream       1   2    3  4  5   Blend
Fraction 10% 60% 20% 10 100%
xa 100 150 175 57 135.0
yg 30.7 22 18 12.2 25.6
aa 210 425 375 307 352.2

表 2(同一个 Excel 工作表中的 range2)

用户输入所有数据,一切都返回到脚本以进行进一步计算:
        Min Max Incr            
temp 45 60 5
press 7.2 7.8 0.2
cf 1 5 1

将数据输入 excel 并传输到脚本后,我就完成了脚本。

我该怎么做呢? Excel 似乎是设置表格条目数据的最简单方法,但如果有其他方法,请告诉我。如果是 Excel,我该怎么做?

最佳答案

这是使用 Pandas 的一种潜在方法。如果输入文件不存在,它会写入一个虚拟文件(修改它以适应),然后打开 excel 文件,并在用户关闭后读回数据框。替换 excel_path带有 Excel 安装路径(我在这里使用 LibreOffice)。

import os
import subprocess

import pandas as pd

input_file = 'input.xlsx'
excel_path = 'soffice'

#############
# setup stuff
#############

if not os.path.isfile(input_file):
input_template = pd.DataFrame(columns=['1','2','3','4'])
# any additional code to set up space for user to input values

input_template.to_excel(input_file)
else:

# call excel and open the input file
subprocess.call([excel_path, input_file])

# read modified input into DataFrame
excel_input = pd.read_excel(input_file)

################
# body of script
################

关于python - 将用户输入的数据从 Excel 工作表传输到 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24353999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com