如何添加“现有”使用 xlrd、xlwt 和 xlutils 将工作表转换为工作簿
如果我理解正确的话,工作簿的 add_sheet
方法会创建一个新的工作表(并将其添加到工作簿中)。我有一个现有的 Excel 模板(带有一个格式化工作表,用作添加信息的基础),我想使用 xlutils 复制该模板,并使用新工作表名称多次将其添加到新工作簿中。我该如何实现这一目标?我浏览了代码以了解如何将现有工作表添加到现有工作簿,但找不到类似的内容?
from xlrd import open_workbook
from xlutils.copy import copy
from xlwt import Workbook
rb = open_workbook('report3.xlt',formatting_info=True)
wb = copy(rb)
new_book = Workbook()
for distinct_employee in distinct_employees:
w_sheet = wb.get_sheet(0)
w_sheet.write(6,6,distinct_employee.name)
# give the sheet a new name (distinct_employee.id_number)
# add this sheet to new_book
book.save('all_employees.xls')
If I understand correctly, a Workbook's add_sheet
method creates a new worksheet (and adds it to the workbook). I have an existing excel template (with one formatted sheet that serves as a base to add information to) that I would like to copy using xlutils and add it to a new Workbook multiple times using new sheet names. How do I go about achieving this? I went through the code to find out how to add an existing worksheet to an existing workbook, but couldn't find anything like that?
from xlrd import open_workbook
from xlutils.copy import copy
from xlwt import Workbook
rb = open_workbook('report3.xlt',formatting_info=True)
wb = copy(rb)
new_book = Workbook()
for distinct_employee in distinct_employees:
w_sheet = wb.get_sheet(0)
w_sheet.write(6,6,distinct_employee.name)
# give the sheet a new name (distinct_employee.id_number)
# add this sheet to new_book
book.save('all_employees.xls')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现使用 copy.deepcopy 您可以创建工作表的副本。另外,使用 _Workbook__worksheets 属性,您可以设置工作簿的工作表列表
使用您的示例,我将拥有以下代码:
I found out that with copy.deepcopy you can create a copy of your woorksheets. Also using the _Workbook__worksheets attribute you can set the list of sheets of your workbook
Using your example I would have the following code: