简体   繁体   English

如何在 Python 中现有的 excel 文件中向特定单元格插入值?

[英]How to insert a value to a specific cell in an existing excel file in Python?

As you can see in the following image, I am trying to insert the string "Latest Price" to cell D1 in an existing sheet "new_sheet" in an excel file.正如您在下图中看到的那样,我正在尝试将字符串“最新价格”插入到 Excel 文件中现有工作表“new_sheet”中的单元格 D1 中。 在此处输入图像描述

My code is as follows.我的代码如下。

import pandas as pd
import openpyxl
from openpyxl import Workbook
from openpyxl import load_workbook

file_source =r'C:\Users\user\Desktop\Data.xlsx'
df = pd.read_excel(file_source)
#load excel file
workbook = load_workbook(filename=file_source)
#Read the sheet "new_sheet"
ws4 = workbook["new_sheet"]
#open workbook
sheet = ws4.active
#modify the desired cell
sheet["D1"] = 'Latest Price'
#save the file
workbook.save(filename=file_source)

However, it showed the following error.但是,它显示了以下错误。

AttributeError: 'Worksheet' object has no attribute 'active'

I can not find the cause for the error.我找不到错误的原因。 Please help me find it.请帮我找到它。

you can update a single cell using the cell.value in openpyxl .您可以使用cell.value中的openpyxl更新单个单元格。 The code is updated as below.代码更新如下。 Let me know in case of issues.如有问题请告诉我。

import pandas as pd
from openpyxl import load_workbook

file_source =r'C:\Users\user\Desktop\Micron_Baseline Cleanup\Data.xlsx'

#load excel file
workbook = load_workbook(filename=file_source)

#Pick the sheet "new_sheet"
ws4 = workbook["new_sheet"]

#modify the desired cell
ws4.cell(row = 1, column = 4).value = 'Latest Price'

#save the file
workbook.save(filename=file_source)

Output excel after running the code运行代码后输出excel

在此处输入图像描述

暂无
暂无

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

相关问题 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文件 - Reading a cell value in Excel and write into an othet existing Excel file with python 如何在Python中将值插入数组中的特定单元格? - How to insert value to specific cell in array in Python? 如何使用python将数据附加到现有excel表的特定单元格? - How to use python to append data to specific cell to existing excel table? 我想使用 python 在特定单元格上的现有 excel 文件中放置一个文本文件 - I would like to put a text file in existing excel file on specific cell using python 如何使用Python将特定的数组值保存到Excel中的单元格? - How do I save a specific array value to a cell in Excel with Python? 如何用python修改现有的Excel单元格颜色 - How to modify the existing Excel cell color with python 如何使用 python 将图像插入特定的 excel 文件单元格? - How to insert images to specific excel file cells using python? 如何在 Sikuli python 上的现有 excel 中插入列? - How to insert a column in an existing excel on Sikuli python? 如何将Pandas Series插入现有Excel文件的特定列中(而不从该文件中删除内容)? - How to insert a Pandas Series into a specific column of an existing Excel file (without deleting content from that file)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM