4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

openpyxlで改ページをしたい

Posted at

目的

openpyxlでグラフ挿入しつつ、グラフの途中で改ページされないようにしたい

ググった

from openpyxl import Workbook 
from openpyxl.worksheet.pagebreak import Break 

wb = Workbook() 
ws = wb.active 
row_number = 20 # the row that you want to insert page break 
page_break = Break(id=row_number) # create Break obj 
ws.page_breaks.append(page_break) # insert page break 

結論

ws.page_breaks[0].append(page_break)

で解決できます。

ドキュメントを確認

class Worksheet(_WorkbookChild):
    ...
    def _setup(self):
        self.row_breaks = RowBreak()
        self.col_breaks = ColBreak()
        self.page_breaks = [self.row_breaks, self.col_breaks]
    ...
class PageBreak(Serialisable):

    tagname = "rowBreaks"

    count = Integer(allow_none=True)
    manualBreakCount = Integer(allow_none=True)
    brk = Sequence(expected_type=Break, allow_none=True)

    __elements__ = ('brk',)
    __attrs__ = ("count", "manualBreakCount",)

    def __init__(self,
                 count=None,
                 manualBreakCount=None,
                 brk=(),
                ):
        self.brk = brk


    def __bool__(self):
        return len(self.brk) > 0

    __nonzero__ = __bool__

    def __len__(self):
        return len(self.brk)


    @property
    def count(self):
        return len(self)


    @property
    def manualBreakCount(self):
        return len(self)


    def append(self, brk=None):
        """
        Add a page break
        """
        vals = list(self.brk)
        if not isinstance(brk, Break):
            brk = Break(id=self.count+1)
        vals.append(brk)
        self.brk = vals



RowBreak = PageBreak
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?