当前位置:网站首页>openpyxl跨工作簿复制sheet页
openpyxl跨工作簿复制sheet页
2022-07-17 07:13:00 【qq_704446162】
查看openpyxl.copy_worksheet 的代码看为啥不支持跨工作簿
只找到了设置的断言:
openpyxl.worksheet.copier.WorksheetCopy :line 32
if self.source.parent != self.target.parent:
raise ValueError('Cannot copy between worksheets from different workbooks')
没发现什么必须同工作簿的操作(水平有限,没看出问题)
所以怀疑是故意不让跨工作簿copy而不是无法复制。
尝试去掉这个断言
测试结果是没有复制插入的图片,不知道不做更改同工作簿复制会不会这样。
再在这个基础上更改有点麻烦,另外感觉整个操作没必要写成类。
提取出动作写成一个def函数,同时加上图片拷贝
def copySheet(source, target):
# 抄写两个断言,一个保证输入对象类型正确
if (not isinstance(source, Worksheet)
and not isinstance(target, Worksheet)):
raise TypeError("Can only copy worksheets")
# 一个保证不是自己复制自己
if source is target:
raise ValueError("Cannot copy a worksheet to itself")
# 复制单元格子数据
for (row, col), source_cell in source._cells.items():
target_cell = target.cell(column=col, row=row)
# 复制数值和数据类型
target_cell._value = source_cell._value
target_cell.data_type = source_cell.data_type
# 复制单元格样式
if source_cell.has_style:
target_cell._style = copy(source_cell._style)
# 复制单元格超链接
if source_cell.hyperlink:
target_cell._hyperlink = copy(source_cell.hyperlink)
# 复制单元格注释批注
if source_cell.comment:
target_cell.comment = copy(source_cell.comment)
# 追加:拷贝插入的图片
target._images = source._images
# 复制行高列宽等信息
for attr in ('row_dimensions', 'column_dimensions'):
src = getattr(source, attr)
dst = getattr(target, attr)
for key, dim in src.items():
dst[key] = copy(dim)
dst[key].worksheet = target
# 这一堆是啥不知道
target.sheet_format = copy(source.sheet_format)
target.sheet_properties = copy(source.sheet_properties)
target.merged_cells = copy(source.merged_cells)
target.page_margins = copy(source.page_margins)
target.page_setup = copy(source.page_setup)
target.print_options = copy(source.print_options)
用法依然效仿之前的方式
借用 openpyxl.Workbook.copy_worksheet
openpyxl/workbook/workbook.py : line 431
本方法依然需要先在 目的工作簿上创建一个sheet
wb0 = load_workbook("src.xlsx")
wb1 = Workbook()
new_title = "new_sheet_title"
to_worksheet = wb1.create_sheet(title=new_title)
copySheet(wb0["src_sheet_title"], to_worksheet)
简单测试了一下,能用。测试文件内容简单,只有插入图片和简单地无格式文本。不清楚复杂sheet复制会不会有问题
边栏推荐
- Facial key point detection CNN
- DP动态规划企业级模板分析(数字三角,上升序列,背包,状态机,压缩DP)
- Standard Version (release and changelog Automation)
- C#对txt文件的读写操作
- Real case: how to check the soaring usage of CPU after the system goes online?
- From the casino logic, analyze the investment value of platform currency 2020-03-03
- 通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
- Forecast sales xgboost
- QT related problems encountered when writing code
- ansible自动化运维详解(四)ansible中playbook的编写使用、执行命令及实例演示
猜你喜欢

Why does the Fed cut interest rates benefit the digital money market in the long run? 2020-03-05

Redis message subscription

The core problem of concurrent programming

總結的太好了!終於有人把SQL的各種連接Join都講明白了

总结的太好了!终于有人把SQL的各种连接Join都讲明白了

Discussion on risc-v Technology

By voting for the destruction of STI by Dao, seektiger is truly community driven

通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动

Redis cluster

写代码遇到Qt相关问题
随机推荐
Xinlinx zynq7020, 7045 domestic replacement fmql45t900 national production arm core board + expansion board
[C# 变量常量关键字]- C# 中的变量常量以及关键字
Paddleserving服务化部署 tensorrt报错, shape of trt subgraph is [-1,-1,768],
The core problem of concurrent programming
一款关于日常习惯打卡的小程序
【Kernel】驱动开发学习之字符设备
Dark horse programmer - software testing -16 stage 3 - function testing -175-198, URL composition introduction, request content and composition description line function test and database, URL composi
Standard Version (release and changelog Automation)
Semiconductor material technology
TextView文字上下移动
Bean、
网传USDT和USDC要做空?带你一探究竟 | Tokenview
With this "programmer code interview guide" from Zuo Chengyun (Zuo Shen), I joined byte
ObjectARX--自定义圆的实现
What if the user information in the website app database is leaked and tampered with
unity 自定义天空球模型防止被裁剪
凭借左程云(左神)的这份 “程序员代码面试指南”我入职了字节
类型详解·自定义类型·结构体初识
812. 最大三角形面积
Go language Bible