复制到特定工作表:openpyxl-使用copy_worksheet时忽略目标工作表 [英] Copying to a specific sheet: openpyxl - destination sheet ignored when using copy_worksheet
本文介绍了复制到特定工作表:openpyxl-使用copy_worksheet时忽略目标工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为此答案和
Per this answer and these documents I tried to specify a source
and target
sheet to write to, but when I do, the results are the same as if I hadn't specified a target:
from openpyxl import load_workbook
wb = load_workbook('MyFile.xlsx')
ws = 'Sheet1'
idx = book.index(ws)
new_ws = 'Test'
book.create_sheet(new_ws, idx+1)
source = book[ws]
target = book[new_ws]
target = book.copy_worksheet(source)
wb.save('Output.xlsx')
vs
source = book[ws]
book.copy_worksheet(source)
wb.save('Output.xlsx')
这两个操作都会在工作簿的末尾添加一个名为Sheet1 Copy
的新工作表. 如何将工作表复制到另一个空白工作表或工作簿中的特定位置?
Both result in a new worksheet called Sheet1 Copy
added to the end of the workbook. How to a copy a sheet into another empty sheet, or specific location within the workbook?
推荐答案
# new copied sheet was assigned to target
target = wb.copy_worksheet(wb.source_sheet)
# now just change the name to desired one
target.title = desired_name
这篇关于复制到特定工作表:openpyxl-使用copy_worksheet时忽略目标工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文