1. Heikki Junes
  2. openpyxl

Commits

Adam Morris  committed 4c9043a

added functionality to print setup

  • Participants
  • Parent commits 7366beb
  • Branches default

Comments (0)

Files changed (3)

File openpyxl/tests/test_worksheet.py

View file
         assert ws.freeze_panes is None
 
     def test_printer_settings(self):
-
         ws = Worksheet(self.wb)
-        ws.set_printer_settings(Worksheet.PAPER_SIZE_LEGAL, Worksheet.ORIENTATION_LANDSCAPE)
+        ws.page_setup.orientation = ws.ORIENTATION_LANDSCAPE
+        ws.page_setup.paperSize = ws.PAPERSIZE_TABLOID
+        ws.page_setup.fitToPage = True
+        ws.page_setup.fitToHeight = 0
+        ws.page_setup.fitToWidth = 1
         xml_string = write_worksheet(ws, None, None)
-        assert '<pageSetup paperSize="5" orientation="landscape"></pageSetup>' in xml_string
+        assert '<pageSetup orientation="landscape" paperSize="3" fitToHeight="0" fitToWidth="1"></pageSetup>' in xml_string
+        assert '<pageSetUpPr fitToPage="1"></pageSetUpPr>' in xml_string
 
         ws = Worksheet(self.wb)
         xml_string = write_worksheet(ws, None, None)
-        assert "<pageSetup paperSize" not in xml_string
+        assert "<pageSetup" not in xml_string
+        assert "<pageSetUpPr" not in xml_string

File openpyxl/worksheet.py

View file
 
 class PageSetup(object):
     """Information about page layout for this sheet"""
-    pass
+    valid_setup = ("orientation", "paperSize", "scale", "fitToPage", "fitToHeight", "fitToWidth", "firstPageNumber", "useFirstPageNumber")
+
+    def __init__(self):
+        self.orientation = self.paperSize = self.scale = self.fitToPage = self.fitToHeight = self.fitToWidth = self.firstPageNumber = self.useFirstPageNumber = None
+
+    @property
+    def setup(self):
+        setupGroup = OrderedDict()
+        for setup_name in self.valid_setup:
+            setup_value = getattr(self, setup_name)
+            if setup_value is not None:
+                if setup_name == 'orientation':
+                    setupGroup[setup_name] = '%s' % setup_value
+                elif setup_name in ('paperSize','scale'):
+                    setupGroup[setup_name] = '%d' % int(setup_value)
+                elif setup_name in ('fitToHeight','fitToWidth') and int(setup_value) >= 0:
+                    setupGroup[setup_name] = '%d' % int(setup_value)
+
+        return setupGroup
+
 
 
 class HeaderFooter(object):
     SHEETSTATE_HIDDEN = 'hidden'
     SHEETSTATE_VERYHIDDEN = 'veryHidden'
 
-    PAPER_SIZE_LETTER = '1'
-    PAPER_SIZE_LEGAL = '5'
+    # Paper size
+    PAPERSIZE_LETTER = '1'
+    PAPERSIZE_LETTER_SMALL = '2'
+    PAPERSIZE_TABLOID = '3'
+    PAPERSIZE_LEDGER = '4'
+    PAPERSIZE_LEGAL = '5'
+    PAPERSIZE_STATEMENT = '6'
+    PAPERSIZE_EXECUTIVE = '7'
+    PAPERSIZE_A3 = '8'
+    PAPERSIZE_A4 = '9'
+    PAPERSIZE_A4_SMALL = '10'
+    PAPERSIZE_A5 = '11'
+
+    # Page orientation
     ORIENTATION_PORTRAIT = 'portrait'
     ORIENTATION_LANDSCAPE = 'landscape'
 

File openpyxl/writer/worksheet.py

View file
     tag(doc, 'outlinePr',
             {'summaryBelow': '%d' % (worksheet.show_summary_below),
             'summaryRight': '%d' % (worksheet.show_summary_right)})
+    if worksheet.page_setup.fitToPage:
+        tag(doc, 'pageSetUpPr', {'fitToPage':'1'})
     end_tag(doc, 'sheetPr')
     tag(doc, 'dimension', {'ref': '%s' % worksheet.calculate_dimension()})
     write_worksheet_sheetviews(doc, worksheet)
     if margins:
         tag(doc, 'pageMargins', margins)
 
-    if worksheet.paper_size is not None:        
-        # size and orientation set in common setter, so expect both or neither
-        tag(doc, 'pageSetup', 
-                {'paperSize': '%s' % (worksheet.paper_size),
-                 'orientation': '%s' % (worksheet.orientation)})
+    setup = worksheet.page_setup.setup
+    if setup:
+        tag(doc, 'pageSetup', setup)
     
     if worksheet._charts:
         tag(doc, 'drawing', {'r:id':'rId1'})