如何添加“现有”使用 xlrd、xlwt 和 xlutils 将工作表转换为工作簿

发布于 2024-10-27 20:06:34 字数 648 浏览 4 评论 0原文

如果我理解正确的话,工作簿的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-11-03 20:06:34

我发现使用 copy.deepcopy 您可以创建工作表的副本。另外,使用 _Workbook__worksheets 属性,您可以设置工作簿的工作表列表

使用您的示例,我将拥有以下代码:

from copy import deepcopy
from xlrd import open_workbook
from xlutils.copy import copy as copy
from xlwt import Workbook
rb = open_workbook('report3.xlt',formatting_info=True)
wb = copy(rb)
new_book = Workbook()

sheets = []
for distinct_employee in distinct_employees:
    w_sheet = deepcopy(wb.get_sheet(0))
    w_sheet.write(6,6,distinct_employee.name)

    # give the sheet a new name (distinct_employee.id_number)
    w_sheet.set_name(distinct_employee.name)

    # add w_sheet  to the sheet list
    sheets.append(w_sheet)

 # set the sheets of the workbook
 new_book._Workbook__worksheets = sheets

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:

from copy import deepcopy
from xlrd import open_workbook
from xlutils.copy import copy as copy
from xlwt import Workbook
rb = open_workbook('report3.xlt',formatting_info=True)
wb = copy(rb)
new_book = Workbook()

sheets = []
for distinct_employee in distinct_employees:
    w_sheet = deepcopy(wb.get_sheet(0))
    w_sheet.write(6,6,distinct_employee.name)

    # give the sheet a new name (distinct_employee.id_number)
    w_sheet.set_name(distinct_employee.name)

    # add w_sheet  to the sheet list
    sheets.append(w_sheet)

 # set the sheets of the workbook
 new_book._Workbook__worksheets = sheets
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文