Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change summary table export orientation to landscape #657

Merged
merged 2 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hawc/apps/summary/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ def build_default(cls, assessment_id: int, table_type: int) -> "SummaryTable":
instance.content = schema.get_default_props()
return instance

def to_docx(self, base_url: str = ""):
def to_docx(self, base_url: str = "", landscape: bool = True):
table = self.get_table()
docx = table.to_docx(parser=QuillParser(base_url=base_url))
docx = table.to_docx(parser=QuillParser(base_url=base_url), landscape=landscape)
return ReportExport(docx=docx, filename=self.slug)

@classmethod
Expand Down
14 changes: 13 additions & 1 deletion hawc/tools/tables/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

from docx import Document as create_document
from docx.document import Document
from docx.enum.section import WD_ORIENT
from docx.shared import Inches
from pydantic import BaseModel, conint

from .parser import QuillParser


def to_landscape(document):
# change docx from portrait to landscape
section = document.sections[0]
new_width, new_height = section.page_height, section.page_width
section.orientation = WD_ORIENT.LANDSCAPE
section.page_width = new_width
section.page_height = new_height


class BaseCell(BaseModel):
header: bool = False
row: conint(ge=0) = 0
Expand Down Expand Up @@ -97,11 +107,13 @@ def validate_cells(self, cells):
def sort_cells(self):
self.cells.sort(key=lambda cell: cell.row_order_index(self.columns))

def to_docx(self, parser: QuillParser = None, docx: Document = None):
def to_docx(self, parser: QuillParser = None, docx: Document = None, landscape: bool = True):
if parser is None:
parser = QuillParser()
if docx is None:
docx = create_document()
if landscape:
to_landscape(docx)
table = docx.add_table(rows=self.rows, cols=self.columns)
table_cells = table._cells
table.style = "Table Grid"
Expand Down
Binary file modified tests/hawc/tools/tables/data/ept_report.docx
Binary file not shown.
Binary file modified tests/hawc/tools/tables/data/generic_report.docx
Binary file not shown.
Binary file modified tests/hawc/tools/tables/data/parsed_html.docx
Binary file not shown.
Binary file modified tests/hawc/tools/tables/data/set_report.docx
Binary file not shown.