我如何使用openpyxl并仍然维护OOP结构?

时间:2017-03-14 03:21:08

标签: python excel oop openpyxl

我正在使用python进行一些模拟并使用openpyxl生成报告。现在模拟结果被分成几张excel文件。根据OOP的原则,我的结构应该有一个实现基本操作的基本模拟器类和几个实现模拟器修改的派生类。由于与类相关的函数应保留在类中,我希望报表单由派生类生成(具有所有样式和格式等)。然后可能是一个驱动程序类或函数,它将所有这些报告表放在一本工作簿中。但据我所知,无法在openpyxl中复制工作表。现在好像我打破了OOP模型。有没有办法解决这个问题?

修改

以下是我的代码示例。这是一个修剪过的无脂版本,真正的类并不那么简单

from openpyxl import Workbook
from openpyxl.styles import Font

class sim1:#this will inherit from another class sim which has basic operations
    def __init__(self):
        #assume function calling and complex math here
        self.x = 1
    def f1OverRide(self):
        #over rides some function in sim to implement custom method for sim1 (several of these)
        return 23
    def get_sheet(self):
        wb = Workbook()
        ws = wb.active
        ws['A1'] = self.x
        #example formatting real formatting is pretty complex
        ws['A1'].font = Font(size=12,name='Calibri')
        return ws

class sim2:#this will inherit from another class sim which has basic operations
    def __init__(self):
        #assume function calling and complex math here
        self.x = 12
    def f1OverRide(self):
        #over rides some function in sim to implement custom method for sim1 (several of these)
        return 42
    def get_sheet(self):
        wb = Workbook()
        ws = wb.active
        ws['A1'] = self.x
        #example formatting, real formatting is pretty complex
        ws['A1'].font = Font(size=14,name='Calibri',color='ff2223')
        return ws


s1 = sim1()
s2 = sim2()
# now I want to get the sheets for sim1 and sim2 and combine in 1 workbook
wb = Workbook()
ws1 = s1.get_sheet()
ws2 = s2.get_sheet()
# dont know what to do now :( openpyxl can not copy sheet into this workbook

1 个答案:

答案 0 :(得分:0)

OOP 复制工作簿之间的工作表,例如:

from openpyxl import Workbook
from openpyxl.styles import Font
from copy import copy

class sim():
    def __init__(self, n):
        self.n = n

    def get_sheet(self):
        #...
        wb = Workbook()
        ws = wb.active
        ws['A1'] = 'sim'+str(self.n)
        if self.n == 1:
            ws['A1'].font = Font(size=12,name='Calibri')
        else:
            ws['A1'].font = Font(size=14, name='Calibri', color='ff2223')
        return ws

class sim_Workbook(Workbook):
    # overload Workbook.copy_worksheet
    def copy_worksheet(self, from_worksheet):
        # Create new empty sheet and append it to self(Workbook)
        ws = self.create_sheet( title=from_worksheet.title )

        for row, row_data in enumerate(from_worksheet.rows,1):
            for column, from_cell in enumerate(row_data,1):
                cell = ws.cell(row=row, column=column)
                cell.value = from_cell.value
                cell.font = copy(from_cell.font)
s1 = sim(1)
s2 = sim(2)

wb = sim_Workbook()
wb.copy_worksheet( s1.get_sheet() )
wb.copy_worksheet( s2.get_sheet() )
wb.save('../test/test.xlsx')  
  

#example格式化真实格式非常复杂

您必须按照示例中font所示的样式复制复杂格式。这可能导致巨大的工作量,具体取决于您需要复制的单元格数量 阅读本文以获得有关此内容的提示,但是当您从工作簿复制到工作簿 copying-styles-from-a-range-to-another-range

时,无法1:1 执行此操作

使用Python测试:3.4.2 - openpyxl:2.4.1 - LibreOffice:4.3.3.2