Python3功能篇八:如何向已有的Excel写数据?

记录一下,方便以后翻阅~
开发环境:PyCharm2019.2.3 社区版
Python版本:3.8

主要代码如下:

# -*- coding: utf-8 -*-
# 作者:闲人Ne
# 格言:学到就要教人,赚到就要给人
# 描述:如何向Excel表里写数据
# 日期: 2020年12月09日

import openpyxl

file_path = 'F:\\5_PyCharm_Python3.8_Workplaces\\20201209_CalLang\\同义词统一化.xlsx'
my_file = openpyxl.open(file_path)                       # 打开目标Excel文件
# sheet1_content1 = my_file.get_sheet_by_name('Sheet1')  # 按sheet名字选择sheet表,也能用,但会有警告! 
sheet1_content1 = my_file['Sheet1']                      # 按sheet名字选择sheet表
sheet1_content1['AA1'] = '序号'                          # 对sheet表的第A列第1行进行操作
my_file.save('同义词统一化.xlsx')                         # 保存

# —————— Copyright (C)2020 闲人Ne. All Rights Reserved —————— END OF FILE —————— #

运行结果:
自己复制代码运行下就明白了~

猜你喜欢

转载自blog.csdn.net/Leisure_ksj/article/details/110928803