用python在Excel工作表中插入指定的值
from openpyxl import Workbookwb=Workbook()#创建一个工作表active_sheet=wb.active#写入单元格数据active_sheet["C4"]=400active_sheet["A1"]=100active_sheet["b2"]=200#写入单元格数据通过制定数字 cell单元格#行和列不能是0active_sheet.cell(row=6,
·
from openpyxl import Workbook
wb=Workbook()
#创建一个工作表
active_sheet=wb.active
#写入单元格数据
active_sheet["C4"]=400
active_sheet["A1"]=100
active_sheet["b2"]=200
#写入单元格数据通过制定数字 cell单元格
#行和列不能是0
active_sheet.cell(row=6,column=5,value=600)
active_sheet.cell(row=5,column=5,value=500)
active_sheet.cell(row=4,column=5,value=400)
#插入公式
active_sheet["e7"]="=e5+e4+e6"
#超链接
active_sheet["C3"]="小度小度"
active_sheet["C3"].hyperlink="http://www.baidu.com"
#批注信息
active_sheet["A3"]="批注"
from openpyxl.comments import Comment
#text:提示内容
#author 谁提示的
active_sheet['A3'].comment=Comment("我是批注信息","itcast")
#插入一行数据
data=["张三","李四","王五","陆六","韩七","腊十"]
# active_sheet.append(data)
#遍历循环来添加数据
#指定某一行
'''
for 数据 in 列表:
pass
for 下标,数据 in enumerate(列表):
pass
'''
for index,item in enumerate(data):
#index 0,1,2,3,4,
#行、列从1 开始
active_sheet.cell(row=20,column=index+1,value=item)
#指定某一列
for index, item in enumerate(data):
active_sheet.cell(row=index + 1, column=10, value=item)
#循环的嵌套
for i in range(1,11): #行循环
for j in range(1,11): #列循环
active_sheet.cell(row=i+8,column=j,value="itcast")
active_sheet["D21"]="我是被修改的值"
#合并单元格 merge_cells
#合并和拆分的单元格一定要对应
active_sheet.merge_cells("m6:p16")
active_sheet.unmerge_cells("m6:p16")
wb.save("insert_values.xlsx")
展示效果:

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)