首页 > Python资料 博客日记
用python复制粘贴excel指定单元格(可保留格式)
2024-03-25 08:00:04Python资料围观29次
本篇文章分享用python复制粘贴excel指定单元格(可保留格式),对你有帮助的话记得收藏一下,看Python资料网收获更多编程知识
近期学习了openpyxl的用法,发现居然没有【复制、粘贴】这么基础的函数。而且若要用python带格式复制粘贴指定区域的单元格,参考资料更少。
于是参考各路大佬的笔记,整合如下。
本代码只完成一次复制粘贴,各位可根据自己的需要加以利用,比如:可搭配遍历文件等实现多个excel中指定区域的复制,并汇总于指定区域的内容。
# 复制区域cell、带格式粘贴: 比如把a1:f16带格式复制粘贴到h23:m38
#导入包
import openpyxl
import copy
#path单引号内放入指定要操作的excel的路径 (本文举例的复制与粘贴,位于同一excel的同一sheet)
path = r'E:\OneDrive\Python_Note\Excel操作\03\反馈表-小寨支行.xlsx'
wb = openpyxl.load_workbook(path)
ws = wb.active #本行代码意思是指定ws为当前在excel中处于选中状态的sheet为ws。
#若excel内有多个sheet,建议使用ws=wb['sheet的名字']
#以字符串输入复制、粘贴的区域,如'a1:f16','h23:m38'(必须大小一致)
Source_Area = 'a1:f16'
Target_Area = 'h23:m38'
#分别指定复制和粘贴所在sheet的位置(本文复制粘贴的单元格区域都在ws内,ws是什么在上面已经指定好)
source_area = ws[Source_Area]
target_area = ws[Target_Area]
#创造source_cell_list,用以和target_cell_list一一对应:
source_cell_list = []
for source_row in source_area:
for source_cell in source_row:
sc_str = str(source_cell)
point_time = sc_str.count('.')
sc_str = sc_str.replace('.', '', point_time - 1)
start = sc_str.find('.')
sc_str = sc_str[start+1 : -1]
source_cell_list.append(sc_str) #提取出单元格编号的字符串,如'C8'
print('source_cell_list:',source_cell_list)
target_cell_list = []
for target_row in target_area:
for target_cell in target_row:
tc_str = str(target_cell)
point_time = tc_str.count('.')
tc_str = tc_str.replace('.', '', point_time - 1)
start = tc_str.find('.')
tc_str = tc_str[start + 1: -1]
target_cell_list.append(tc_str) # 提取出单元格编号的字符串,如'L10'
print('target_cell_list:',target_cell_list)
#获取要复制的单元格总个数:
cells = len(source_cell_list)
#提取并复制格式:
i=0
while i<=cells-1:
ws[target_cell_list[0+i]].data_type = ws[source_cell_list[0+i]].data_type
if ws[source_cell_list[0+i]].has_style:
ws[target_cell_list[0+i]]._style = copy.copy(ws[source_cell_list[0+i]]._style)
ws[target_cell_list[0+i]].font = copy.copy(ws[source_cell_list[0+i]].font)
ws[target_cell_list[0+i]].border = copy.copy(ws[source_cell_list[0+i]].border)
ws[target_cell_list[0+i]].fill = copy.copy(ws[source_cell_list[0+i]].fill)
ws[target_cell_list[0+i]].number_format = copy.copy(ws[source_cell_list[0+i]].number_format)
ws[target_cell_list[0+i]].protection = copy.copy(ws[source_cell_list[0+i]].protection)
ws[target_cell_list[0+i]].alignment = copy.copy(ws[source_cell_list[0+i]].alignment)
# 通过引用方法粘贴值: ws['']=ws[''].value
ws[target_cell_list[0+i]] = ws[source_cell_list[0+i]].value
i+=1
#保存更改:(若复制粘贴来源于不同文件要分别保存)
wb.save(r'E:\OneDrive\Python_Note\Excel操作\03\反馈表-小寨支行-py改.xlsx')
再补充一下如何遍历到某个文件夹下所有的excel。这样就可以实现批量自动提取每个excel的指定位置的数据了:
#插入库(同样库只用插入一遍)
import openpyxl
import os
#指定文件夹路径:
path = r'E:\OneDrive\Python_Note\Excel操作'
#使用for循环对文件夹内的每个excel进行相同操作:
for file_name in os.listdir(path):
if file_name.endswith('.xlsx') or file_name.endswith(".xls"):
#本次循环对象excel文件的绝对路径:
excel = path + '\\' + file_name
print('开始执行:',excel)
wb = openpyxl.load_workbook(excel)
#以下开始是在每个excel内的所需操作(根据自己需要)
ws = wb.active #指定sheet名,推荐操作的excel都只有一个sheet时用,可以解决各excel里sheet名称不同的问题。
Source_Area = 'a1:f16'
Target_Area = 'h23:m38'
source_area = ws[Source_Area]
target_area = ws[Target_Area]
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- Python淘宝书籍图书销售数据爬虫可视化分析大屏全屏系统 开题报告
- Python中NumPy库提供的函数——np.random.randn的基本用法
- 从静态到动态化,Python数据可视化中的Matplotlib和Seaborn
- 爬虫实战+数据分析:全国消费支出分析及未来预测
- aardio封装库) sunny抓包工具的使用
- Python广东广州二手房源爬虫数据可视化分析大屏全屏系统 开题报告
- AttributeError: ‘DataFrame‘ object has no attribute ‘iteritems‘解决方案【Bug已解决-Python】
- Microsoft SQL Server 编写汉字转拼音函数
- 用python复制粘贴excel指定单元格(可保留格式)
- 【头歌-Python】Python第一章作业(初级)
点击排行
- PyPy JIT编译器背后的奥秘(揭开PyPy高性能Python的编译优化技术)
- Python数字ceil()方法
- PyCharm:提升Python开发效率的强大IDE工具
- Numba:无缝将Python代码编译为机器代码的利器(让你的Python程序跑的飞快!Numba高效编译的6大技巧)
- 彻底理解Global Interpreter Lock,解锁Python多线程编程(为什么Python多线程速度这么慢)
- Ruff更新到v0.0.281(Rust 编写的高性能 Python 代码分析工具)
- 8月16日魔珐科技发布了三款超写实3D虚拟人产品掀起AIGC「新工业革命」
- PyPy与CPython扩展库的兼容性问题及优化方案 (揭秘PyPy如何与C扩展无缝衔接,5个兼容性要点让你易如反掌)