繁体   English   中英

Python Excel:如何向现有 excel 文件中的每个单元格添加一个数字,然后将新值发送回 excel 文件,然后将新值发送回 excel 文件,然后将新值发送回 excel 文件,然后将新值发送回 excel 文件

[英]Python Excel: how to add a number to each cell in an existing excel file and then send the new value back to excel document

如何为现有 Excel 文件中的每个单元格添加一个数字,然后将新值发送回 Python 中的 excel 文档?

这是我目前拥有的:

for infile in glob2.glob("*.xlsx"):
    file, ext = os.path.splitext(infile)
    print(file)

Filename=str(file)+'.xlsx'
df=pd.read_excel(Filename, index_col=0,parse_dates=True)

#Tell it what columns to read from each file
vals41 = (df['IN/OUT4_1'])
vals41 =(df['IN/OUT4_2'])
vals43=(df['IN/OUT4_3']) 

我想将 14.7 添加到这 3 列中的每个单元格,然后将新值发送回文件。

如果我正确理解了您的要求,您正在尝试更新 excel 中 3 个特定列的值。 您可以尝试以下代码:

for infile in glob2.glob("*.xlsx"):
    file, ext = os.path.splitext(infile)
    print(file)

Filename=str(file)+'.xlsx'
df=pd.read_excel(Filename, index_col=0,parse_dates=True)

#Tell it what columns to read from each file
cols=['IN/OUT4_1','IN/OUT4_2','IN/OUT4_3'] 
df[cols] = df[cols]+14.7

df.to_excel(r'Path to store the exported excel file\File Name.xlsx', index = False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM