python - 使用 xlwings 和 python 复制工作表

标签 python excel xlwings

我一直在 Python 中使用 xlwings,但一直无法弄清楚如何复制工作表。我想将特定工作表视为模板,并在每次修改之前复制该工作表。

我正在使用 xlwings 的 0.11.4 版本。如果没有内置此类功能,我可以在 xlwings 之外使用 pywin32 函数来完成此操作。

最佳答案

在多处查找和阅读pywin32文档后,我找到了复制工作表的解决方案:

import xlwings as xw
wb = xw.Book('filename.xlsx')
sheet = wb.sheets['Sheet1']

#copy within the same sheet
sheet.api.Copy(Before=sheet.api)

#copy to a new workbook
sheet.api.Copy()

#copy a third time at the beginning of the sheets
sheet2 = wb.sheets['sheet1 (2)']
sheet.api.Copy(Before=sheet2.api)

#copy to an existing workbook by putting it in front of a worksheet object
sheet.api.Copy(Before=existingSheet.api)

确实超出了 xlwings 提供的 native 功能。因为 xlwings 是 pywin32 的包装器,.api() 调用允许访问 xlwings 中未记录的那些 pywin32 函数。

另请注意,“After”命令在工作表中不起作用;它将打开一个复制了工作表的新工作簿。这应该不会造成太大的问题,因为我相信如果需要可以重新排序索引。

关于python - 使用 xlwings 和 python 复制工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45948255/

相关文章:

python - 在哪里放置已编译 Python 脚本的配置文件?

vba - 为 VBA 编辑器选择字体

python - 在Excel上获取python XLwings用户定义函数以定期运行

python - Python 中将元素添加到逗号分隔字符串的有效方法

python - pandas.Timestamp 是不可变的吗?

python - 使用 pip3 : module "importlib._bootstrap" has no attribute "SourceFileLoader"

excel - VBA使Excel公式更具可读性

database - 创建自定义订单系统

python - 无论我做什么,都无法将 XLWings 加载项安装到 excel 中

python - float 转换为从 .dat 文件读取的符号。不确定编码