Adam Morris avatar Adam Morris committed 5c0c86c Merge

Merged ericgazoni/openpyxl/1.8 into 1.8

Comments (0)

Files changed (6)

openpyxl/sample/chart_gen.py

-"""Simple test charts"""
-from datetime import date
-import os
-from openpyxl import Workbook
-from openpyxl.chart import Chart, Serie, Reference, BarChart, PieChart, LineChart, ScatterChart
-
-wb = Workbook()
-ws = wb.get_active_sheet()
-ws.title = "Numbers"
-for i in range(10):
-    ws.append([i])
-chart = BarChart()
-values = Reference(ws, (0, 0), (9, 0))
-series = Serie(values)
-chart.add_serie(series)
-ws.add_chart(chart)
-
-ws = wb.create_sheet(1, "Negative")
-for i in range(-5, 5):
-    ws.append([i])
-chart = BarChart()
-values = Reference(ws, (0, 0), (9, 0))
-series = Serie(values)
-chart.add_serie(series)
-ws.add_chart(chart)
-
-ws = wb.create_sheet(2, "Letters")
-for idx, l in enumerate("ABCDEFGHIJ"):
-    ws.append([l, idx, idx])
-chart = BarChart()
-labels = Reference(ws, (0, 0), (9, 0))
-values = Reference(ws, (0, 1), (9, 1))
-series = Serie(values, labels=labels)
-chart.add_serie(series)
-#  add second series
-values = Reference(ws, (0, 2), (9, 2))
-series = Serie(values, labels=labels)
-chart.add_serie(series)
-ws.add_chart(chart)
-
-ws = wb.create_sheet(3, "Dates")
-for i in range(1, 10):
-    ws.append([date(2013, i, 1), i])
-chart = BarChart()
-values = Reference(ws, (0, 1), (9, 1))
-labels = Reference(ws, (0, 0), (9, 0))
-labels.number_format = 'd-mmm'
-series = Serie(values, labels=labels)
-chart.add_serie(series)
-ws.add_chart(chart)
-
-ws = wb.create_sheet(4, "Pie")
-for i in range(1, 5):
-    ws.append([i])
-chart = PieChart()
-values = Reference(ws, (0, 0), (9, 0))
-series = Serie(values, labels=values)
-chart.add_serie(series)
-ws.add_chart(chart)
-
-ws = wb.create_sheet(5, "Line")
-for i in range(1, 5):
-    ws.append([i])
-chart = LineChart()
-values = Reference(ws, (0, 0), (4,0))
-series = Serie(values)
-chart.add_serie(series)
-ws.add_chart(chart)
-
-ws = wb.create_sheet(6, "Scatter")
-for i in range(10):
-    ws.append([i, i])
-chart = ScatterChart()
-xvalues = Reference(ws, (0, 1), (10, 1))
-values = Reference(ws, (0, 0), (10, 0))
-series = Serie(values, xvalues=xvalues)
-chart.add_serie(series)
-ws.add_chart(chart)
-
-folder = os.path.split(__file__)[0]
-wb.save(os.path.join(folder, "files", "charts_gen.xlsx"))

openpyxl/sample/charts.py

+"""Simple test charts"""
+from datetime import date
+import os
+from openpyxl import Workbook
+from openpyxl.chart import Chart, Serie, Reference, BarChart, PieChart, LineChart, ScatterChart
+
+
+def numbers(wb):
+    ws = wb.create_sheet(1, "Numbers")
+    for i in range(10):
+        ws.append([i])
+    chart = BarChart()
+    values = Reference(ws, (0, 0), (9, 0))
+    series = Serie(values)
+    chart.add_serie(series)
+    ws.add_chart(chart)
+
+def negative(wb):
+    ws = wb.create_sheet(1, "Negative")
+    for i in range(-5, 5):
+        ws.append([i])
+    chart = BarChart()
+    values = Reference(ws, (0, 0), (9, 0))
+    series = Serie(values)
+    chart.add_serie(series)
+    ws.add_chart(chart)
+
+def letters(wb):
+    ws = wb.create_sheet(2, "Letters")
+    for idx, l in enumerate("ABCDEFGHIJ"):
+        ws.append([l, idx, idx])
+    chart = BarChart()
+    labels = Reference(ws, (0, 0), (9, 0))
+    values = Reference(ws, (0, 1), (9, 1))
+    series = Serie(values, labels=labels)
+    chart.add_serie(series)
+    #  add second series
+    values = Reference(ws, (0, 2), (9, 2))
+    series = Serie(values, labels=labels)
+    chart.add_serie(series)
+    ws.add_chart(chart)
+
+def dates(wb):
+    ws = wb.create_sheet(3, "Dates")
+    for i in range(1, 10):
+        ws.append([date(2013, i, 1), i])
+    chart = BarChart()
+    values = Reference(ws, (0, 1), (9, 1))
+    labels = Reference(ws, (0, 0), (9, 0))
+    labels.number_format = 'd-mmm'
+    series = Serie(values, labels=labels)
+    chart.add_serie(series)
+    ws.add_chart(chart)
+
+def pie(wb):
+    ws = wb.create_sheet(4, "Pie")
+    for i in range(1, 5):
+        ws.append([i])
+    chart = PieChart()
+    values = Reference(ws, (0, 0), (9, 0))
+    series = Serie(values, labels=values)
+    chart.add_serie(series)
+    ws.add_chart(chart)
+
+def line(wb):
+    ws = wb.create_sheet(5, "Line")
+    for i in range(1, 5):
+        ws.append([i])
+    chart = LineChart()
+    values = Reference(ws, (0, 0), (4,0))
+    series = Serie(values)
+    chart.add_serie(series)
+    ws.add_chart(chart)
+
+def scatter(wb):
+    ws = wb.create_sheet(6, "Scatter")
+    for i in range(10):
+        ws.append([i, i])
+    chart = ScatterChart()
+    xvalues = Reference(ws, (0, 1), (10, 1))
+    values = Reference(ws, (0, 0), (10, 0))
+    series = Serie(values, xvalues=xvalues)
+    chart.add_serie(series)
+    ws.add_chart(chart)
+
+if __name__ == "__main__":
+    wb = Workbook()
+    ws = wb.get_active_sheet()
+    wb.remove_sheet(ws)
+    numbers(wb)
+    negative(wb)
+    letters(wb)
+    dates(wb)
+    pie(wb)
+    line(wb)
+    scatter(wb)
+
+    folder = os.path.split(__file__)[0]
+    wb.save(os.path.join(folder, "files", "charts.xlsx"))
Add a comment to this file

openpyxl/sample/files/charts.xlsx

Binary file added.

Add a comment to this file

openpyxl/sample/files/charts_gen.xlsx

Binary file removed.

openpyxl/tests/test_drawing.py

 
     def setup(self):
         from openpyxl.writer.drawings import DrawingWriter
-        from openpyxl.drawing import Drawing
         sheet = DummySheet()
-        chart = DummyChart()
-        d = Drawing()
-        chart.drawing = d
-        sheet._charts = [chart]
+        sheet._charts = []
         sheet._images = []
         self.dw = DrawingWriter(sheet=sheet)
 
     def test_write(self):
         xml = self.dw.write()
-        expected = """<xdr:wsDr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">
-  <xdr:absoluteAnchor>
-    <xdr:pos x="0" y="0" />
-    <xdr:ext cx="200025" cy="1828800" />
-    <xdr:graphicFrame macro="">
-      <xdr:nvGraphicFramePr>
-        <xdr:cNvPr id="0" name="Graphique 0" />
-        <xdr:cNvGraphicFramePr />
-      </xdr:nvGraphicFramePr>
-      <xdr:xfrm>
-        <a:off x="0" y="0" />
-        <a:ext cx="0" cy="0" />
-      </xdr:xfrm>
-      <a:graphic>
-        <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart">
-          <c:chart r:id="rId1" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" />
-        </a:graphicData>
-      </a:graphic>
-    </xdr:graphicFrame>
-    <xdr:clientData />
-  </xdr:absoluteAnchor>
+        expected = """<xdr:wsDr xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">
 </xdr:wsDr>"""
         diff = compare_xml(xml, expected)
         assert diff is None, diff
         chart = DummyChart()
         drawing = Drawing()
         chart.drawing = drawing
-        self.dw._write_chart(root, chart, 0)
+        self.dw._write_chart(root, chart, 1)
         xml = get_xml(root)
         expected = """<xdr:test xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
         xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart">
     <xdr:ext cx="200025" cy="1828800"/>
     <xdr:graphicFrame macro="">
       <xdr:nvGraphicFramePr>
-        <xdr:cNvPr id="0" name="Graphique 0"/>
+        <xdr:cNvPr id="2" name="Chart 1"/>
         <xdr:cNvGraphicFramePr/>
       </xdr:nvGraphicFramePr>
       <xdr:xfrm>
         path = os.path.join(DATADIR, "plain.png")
         img = Image(path)
         root = Element("test")
-        self.dw._write_image(root, img, 0)
+        self.dw._write_image(root, img, 1)
         xml = get_xml(root)
         expected = """<test xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">
   <xdr:absoluteAnchor>
     <xdr:ext cx="1123950" cy="1123950"/>
     <xdr:pic>
       <xdr:nvPicPr>
-        <xdr:cNvPr id="0" name="Picture 0"/>
+        <xdr:cNvPr id="2" name="Picture 1"/>
         <xdr:cNvPicPr>
           <a:picLocks noChangeArrowheads="1" noChangeAspect="1"/>
         </xdr:cNvPicPr>
 
 
     def test_write_rels(self):
-        from openpyxl.shared.xmltools import Element
-        xml = self.dw.write_rels(0, 0)
+        self.dw._sheet._charts.append(None)
+        self.dw._sheet._images.append(None)
+        xml = self.dw.write_rels(1, 1)
         expected = """<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
-             <Relationship Id="rId1" Target="../charts/chart0.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" />
-           </Relationships>"""
+  <Relationship Id="rId1" Target="../charts/chart1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart"/>
+  <Relationship Id="rId2" Target="../media/image1.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"/>
+</Relationships>
+"""
         diff = compare_xml(xml, expected)
         assert diff is None, diff
 

openpyxl/writer/drawings.py

         root = Element("{%s}wsDr" % SHEET_DRAWING_NS)
 
         for idx, chart in enumerate(self._sheet._charts):
-            self._write_chart(root, chart, idx)
+            self._write_chart(root, chart, idx+1)
 
         for idx, img in enumerate(self._sheet._images):
-            self._write_image(root, img, idx)
+            self._write_image(root, img, idx+1)
 
         return get_document_content(root)
 
         frame = SubElement(anchor, '{%s}graphicFrame' % SHEET_DRAWING_NS, {'macro':''})
 
         name = SubElement(frame, '{%s}nvGraphicFramePr' % SHEET_DRAWING_NS)
-        SubElement(name, '{%s}cNvPr'% SHEET_DRAWING_NS, {'id':'%s' % idx, 'name':'Graphique %s' % idx})
+        SubElement(name, '{%s}cNvPr'% SHEET_DRAWING_NS, {'id':'%s' % (idx + 1), 'name':'Chart %s' % idx})
         SubElement(name, '{%s}cNvGraphicFramePr' % SHEET_DRAWING_NS)
 
         frm = SubElement(frame, '{%s}xfrm'  % SHEET_DRAWING_NS)
 
         graph = SubElement(frame, '{%s}graphic' % DRAWING_NS)
         data = SubElement(graph, '{%s}graphicData' % DRAWING_NS, {'uri':CHART_NS})
-        SubElement(data, '{%s}chart' % CHART_NS, {'{%s}id' % REL_NS:'rId%s' % (idx + 1)})
+        SubElement(data, '{%s}chart' % CHART_NS, {'{%s}id' % REL_NS:'rId%s' % idx })
 
         SubElement(anchor, '{%s}clientData' % SHEET_DRAWING_NS)
         return node
 
         pic = SubElement(anchor, '{%s}pic' % SHEET_DRAWING_NS)
         name = SubElement(pic, '{%s}nvPicPr' % SHEET_DRAWING_NS)
-        SubElement(name, '{%s}cNvPr' % SHEET_DRAWING_NS, {'id':'%s' % idx, 'name':'Picture %s' % idx})
+        SubElement(name, '{%s}cNvPr' % SHEET_DRAWING_NS, {'id':'%s' % (idx + 1), 'name':'Picture %s' % idx})
         SubElement(SubElement(name, '{%s}cNvPicPr' % SHEET_DRAWING_NS),
                    '{%s}picLocks' % DRAWING_NS, {'noChangeAspect':"1" if img.nochangeaspect\
                                     else '0','noChangeArrowheads':"1" if img.nochangearrowheads\
                                     else '0'})
         blipfill = SubElement(pic, '{%s}blipFill' % SHEET_DRAWING_NS)
         SubElement(blipfill, '{%s}blip' % DRAWING_NS, {
-            '{%s}embed' % REL_NS: 'rId%s' % (idx + 1),
+            '{%s}embed' % REL_NS: 'rId%s' % idx,
             'cstate':'print'
         })
         SubElement(blipfill, '{%s}srcRect' % DRAWING_NS)
                 'Target' : '../charts/chart%s.xml' % (chart_id + i) }
             SubElement(root, '{%s}Relationship' % PKG_REL_NS, attrs)
         for j, img in enumerate(self._sheet._images):
-            attrs = {'Id' : 'rId%s' % (i + j + 1),
-                'Type' : '{%s/image' % REL_NS,
+            attrs = {'Id' : 'rId%s' % (i + 1 + j + 1),
+                'Type' : '%s/image' % REL_NS,
                 'Target' : '../media/image%s.png' % (image_id + j) }
             SubElement(root, '{%s}Relationship' % PKG_REL_NS, attrs)
         return get_document_content(root)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.