Commits

aceMueller  committed 033373a

add function to set page orientation and paper size, including test cases

  • Participants
  • Parent commits 61f00f0

Comments (0)

Files changed (3)

File openpyxl/tests/test_worksheet.py

 from openpyxl.cell import Cell
 from openpyxl.shared.exc import CellCoordinatesException, \
         SheetTitleException, InsufficientCoordinatesException, \
-        NamedRangeException
-
+        NamedRangeException 
+from openpyxl.writer.worksheet import write_worksheet
 
 class TestWorksheet(object):
 
         ws.freeze_panes = ws.cell('A1')
         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)
+        xml_string = write_worksheet(ws, None, None)
+        assert '<pageSetup paperSize="5" orientation="landscape"></pageSetup>' in xml_string
+
+        ws = Worksheet(self.wb)
+        xml_string = write_worksheet(ws, None, None)
+        assert "<pageSetup paperSize" not in xml_string        
+        

File openpyxl/worksheet.py

     SHEETSTATE_HIDDEN = 'hidden'
     SHEETSTATE_VERYHIDDEN = 'veryHidden'
 
+    PAPER_SIZE_LETTER = '1'
+    PAPER_SIZE_LEGAL = '5'
+    ORIENTATION_PORTRAIT = 'portrait'
+    ORIENTATION_LANDSCAPE = 'landscape'
+    
     def __init__(self, parent_workbook, title='Sheet'):
         self._parent = parent_workbook
         self._title = ''
         self.default_column_dimension = ColumnDimension()
         self._auto_filter = None
         self._freeze_panes = None
+        self.paper_size = None
+        self.orientation = None
 
     def __repr__(self):
         return '<Worksheet "%s">' % self.title
             self._styles[coordinate] = Style()
         return self._styles[coordinate]
 
+    def set_printer_settings(self, paper_size, orientation):
+        """Set printer settings """
+        
+        self.paper_size = paper_size
+        self.orientation = orientation
+    
     def create_relationship(self, rel_type):
         """Add a relationship for this sheet."""
         rel = Relationship(rel_type)

File openpyxl/writer/worksheet.py

     if worksheet.auto_filter:
         tag(doc, 'autoFilter', {'ref': worksheet.auto_filter})
     write_worksheet_hyperlinks(doc, worksheet)
+    
+    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}) 
+    
     if worksheet._charts:
         tag(doc, 'drawing', {'r:id':'rId1'})
     end_tag(doc, 'worksheet')