翼度科技»论坛 编程开发 python 查看内容

如何用python复制粘贴excel指定单元格(可保留格式)

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
近期学习了openpyxl的用法,发现居然没有【复制、粘贴】这么基础的函数。而且若要用python带格式复制粘贴指定区域的单元格,参考资料更少。
于是参考各路大佬的笔记,整合如下。
本代码只完成一次复制粘贴,各位可根据自己的需要加以利用,比如:可搭配遍历文件等实现多个excel中指定区域的复制,并汇总于指定区域的内容。
  1. # 复制区域cell、带格式粘贴: 比如把a1:f16带格式复制粘贴到h23:m38

  2. #导入包
  3. import openpyxl
  4. import copy

  5. #path单引号内放入指定要操作的excel的路径 (本文举例的复制与粘贴,位于同一excel的同一sheet)
  6. path = r'E:\OneDrive\Python_Note\Excel操作\03\反馈表-小寨支行.xlsx'
  7. wb = openpyxl.load_workbook(path)
  8. ws = wb.active  #本行代码意思是指定ws为当前在excel中处于选中状态的sheet为ws。
  9.                 #若excel内有多个sheet,建议使用ws=wb['sheet的名字']

  10. #以字符串输入复制、粘贴的区域,如'a1:f16','h23:m38'(必须大小一致)
  11. Source_Area = 'a1:f16'  
  12. Target_Area = 'h23:m38'

  13. #分别指定复制和粘贴所在sheet的位置(本文复制粘贴的单元格区域都在ws内,ws是什么在上面已经指定好)
  14. source_area = ws[Source_Area]   
  15. target_area = ws[Target_Area]  

  16. #创造source_cell_list,用以和target_cell_list一一对应:
  17. source_cell_list = []
  18. for source_row in source_area:
  19.     for source_cell in source_row:
  20.         sc_str = str(source_cell)  
  21.         point_time = sc_str.count('.')
  22.         sc_str = sc_str.replace('.', '', point_time - 1)
  23.         start = sc_str.find('.')
  24.         sc_str = sc_str[start+1 : -1]
  25.         source_cell_list.append(sc_str) #提取出单元格编号的字符串,如'C8'
  26. print('source_cell_list:',source_cell_list)
  27. target_cell_list = []
  28. for target_row in target_area:
  29.     for target_cell in target_row:
  30.         tc_str = str(target_cell)  
  31.         point_time = tc_str.count('.')
  32.         tc_str = tc_str.replace('.', '', point_time - 1)
  33.         start = tc_str.find('.')
  34.         tc_str = tc_str[start + 1: -1]
  35.         target_cell_list.append(tc_str)  # 提取出单元格编号的字符串,如'L10'
  36. print('target_cell_list:',target_cell_list)

  37. #获取要复制的单元格总个数:
  38. cells = len(source_cell_list)

  39. #提取并复制格式:
  40. i=0
  41. while i<=cells-1:
  42.     ws[target_cell_list[0+i]].data_type = ws[source_cell_list[0+i]].data_type
  43.     if ws[source_cell_list[0+i]].has_style:
  44.         ws[target_cell_list[0+i]]._style = copy.copy(ws[source_cell_list[0+i]]._style)
  45.         ws[target_cell_list[0+i]].font = copy.copy(ws[source_cell_list[0+i]].font)
  46.         ws[target_cell_list[0+i]].border = copy.copy(ws[source_cell_list[0+i]].border)
  47.         ws[target_cell_list[0+i]].fill = copy.copy(ws[source_cell_list[0+i]].fill)
  48.         ws[target_cell_list[0+i]].number_format = copy.copy(ws[source_cell_list[0+i]].number_format)
  49.         ws[target_cell_list[0+i]].protection = copy.copy(ws[source_cell_list[0+i]].protection)
  50.         ws[target_cell_list[0+i]].alignment = copy.copy(ws[source_cell_list[0+i]].alignment)
  51.     # 通过引用方法粘贴值: ws['']=ws[''].value
  52.     ws[target_cell_list[0+i]] = ws[source_cell_list[0+i]].value
  53.     i+=1

  54. #保存更改:(若复制粘贴来源于不同文件要分别保存)
  55. wb.save(r'E:\OneDrive\Python_Note\Excel操作\03\反馈表-小寨支行-py改.xlsx')
复制代码
再补充一下如何遍历到某个文件夹下所有的excel。这样就可以实现批量自动提取每个excel的指定位置的数据了:
  1. #插入库(同样库只用插入一遍)
  2. import openpyxl
  3. import os

  4. #指定文件夹路径:
  5. path = r'E:\OneDrive\Python_Note\Excel操作'

  6. #使用for循环对文件夹内的每个excel进行相同操作:

  7. for file_name in os.listdir(path):
  8.     if file_name.endswith('.xlsx') or file_name.endswith(".xls"):
  9.         #本次循环对象excel文件的绝对路径:
  10.         excel = path + '\\' + file_name
  11.         print('开始执行:',excel)
  12.         wb = openpyxl.load_workbook(excel)

  13.         #以下开始是在每个excel内的所需操作(根据自己需要)
  14.         
  15.         ws = wb.active #指定sheet名,推荐操作的excel都只有一个sheet时用,可以解决各excel里sheet名称不同的问题。

  16.         Source_Area = 'a1:f16'  
  17.         Target_Area = 'h23:m38'     
  18.         source_area = ws[Source_Area]   
  19.         target_area = ws[Target_Area]
复制代码
总结
到此这篇关于如何用python复制粘贴excel指定单元格的文章就介绍到这了,更多相关python复制粘贴excel指定单元格内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/python/292080rxl.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具