![](/img/trans.png)
[英]Python Excel: How to add a cell value and a constant and then print back to the 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.