Skip to content

Added support for table slides #4

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

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*.gem
*.rbc
.bundle
*.bundle
.config
.yardoc
Gemfile.lock
Expand All @@ -15,3 +15,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
#*
*~
2 changes: 2 additions & 0 deletions lib/powerpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
require 'powerpoint/slide/intro'
require 'powerpoint/slide/textual'
require 'powerpoint/slide/pictorial'
require 'powerpoint/slide/table'
require 'powerpoint/relationship'
require 'powerpoint/slide/relationship'
require 'powerpoint/content_type'
require 'powerpoint/meta'
require 'powerpoint/presentation'
require 'powerpoint/utils/table'

module Powerpoint

Expand Down
17 changes: 13 additions & 4 deletions lib/powerpoint/presentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@ def add_textual_slide title, content=[]
@slide_count += 1
Powerpoint::Slide::Textual.new @extract_path, title, content, @slide_count
Powerpoint::Slide::Relationship.new @extract_path, @slide_count
setablish_relationships
establish_relationships
end

def add_table_slide title, matrix, options={}
@slide_count += 1
table = Powerpoint::Utils::Table.new matrix, options

Powerpoint::Slide::Table.new @extract_path, title, table, @slide_count
Powerpoint::Slide::Relationship.new @extract_path, @slide_count
establish_relationships
end

def add_pictorial_slide title, image_path, coords={}
@slide_count += 1
Powerpoint::Slide::Pictorial.new @extract_path, title, image_path, @slide_count, coords
setablish_relationships
establish_relationships
end

def setablish_relationships
def establish_relationships
if @slide_count > 2
Powerpoint::ContentType.new @extract_path, @slide_count
Powerpoint::Relationship.new @extract_path, @slide_count
Expand All @@ -47,4 +56,4 @@ def save path
path
end
end
end
end
6 changes: 3 additions & 3 deletions lib/powerpoint/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

module Powerpoint
class Powerpoint::Relationship
def initialize extract_path, sllide_number
xml = '<Relationship Id="rId'+ (sllide_number+666).to_s + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide' + sllide_number.to_s + '.xml"/></Relationships>'
def initialize extract_path, slide_number
xml = '<Relationship Id="rId'+ (slide_number+666).to_s + '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide' + slide_number.to_s + '.xml"/></Relationships>'
path = "#{extract_path}/ppt/_rels/presentation.xml.rels"
template = File.read path
template.gsub!('</Relationships>', xml)
File.open(path, 'w'){ |f| f << template }
end
end
end
end
23 changes: 23 additions & 0 deletions lib/powerpoint/slide/table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'zip/filesystem'
require 'fileutils'

module Powerpoint
module Slide
class Powerpoint::Slide::Table
def initialize extract_path, title, table, slide_number
template_path = "#{TEMPLATE_PATH}/ppt/slides/slide3.xml"
xml = File.read template_path

#set the title
xml.gsub! "TITLE_PLACEHOLDER", title

#set the table
xml.gsub! "TABLE_PLACEHOLDER", table.to_xml

#write the result to the powerpoint
slide_path = "#{extract_path}/ppt/slides/slide#{slide_number}.xml"
File.open(slide_path, "w") { |f| f << xml }
end
end
end
end
181 changes: 181 additions & 0 deletions lib/powerpoint/utils/table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@

module Powerpoint
module Utils
class Table
def initialize matrix, settings = {}
#assertions
raise ArgumentError.new("no matrix") unless matrix #not null
raise ArgumentError.new("no rows") if matrix.first.nil? #has rows
raise ArgumentError.new("no cols") if matrix.first.first.nil? #has cols

#actual rows and cols
@matrix = matrix

#number of cols...used for the tblgrid section
@ncols = matrix.first.length

#column width
#TODO: specify individual rows
#TODO: human readable widths
@width = (settings[:width] or "1645920")

#row height
#TODO: human readable heights
@height = (settings[:height] or "370840")

#table style by microsoft id
@style = name_to_guid((settings[:style] or :medium_style_2_accent_1))

#boolean...first row is a header
#TODO: actually covert this to a ruby bool
@firstrow = (settings[:firstrow] or "1")

#boolean...row color bands
#TODO: actually convert this to a ruby bool
@bandrow = (settings[:bandrow] or "1")

end

#setters
def width=(width)
@width = width
end

def style=(style)
@style = style
end

def firstrow=(firstrow)
@firstrow = firstrow
end

def bandrow=(bandrow)
@bandrow = bandrow
end

#renders the xml representing the table...gets used in the Slide::Table class to render xml
def to_xml
%{
<a:tbl>
<a:tblPr firstRow="#{@firstrow}" bandRow="">
<a:tableStyleId>{#{@style}}</a:tableStyleId>
</a:tblPr>
<a:tblGrid>
#{
"<a:gridCol w='#{@width}' />"*@ncols
}
</a:tblGrid>
#{
@matrix.map do |row|
%{
<a:tr h="#{@height}">
#{
row.map do |col|
%{
<a:tc>
<a:txBody>
<a:bodyPr />
<a:lstStyle />
<a:p>
<a:r>
<a:rPr lang="en-US" dirty="0" smtClean="0" />
<a:t>#{col}</a:t>
</a:r>
<a:endParaRPr lang="en-US" dirty="0" />
</a:p>
</a:txBody>
<a:tcPr />
</a:tc>
}
end.join
}
</a:tr>
}
end.join
}
</a:tbl>
}
end

private
def name_to_guid(name)
{
no_style_no_grid: "2D5ABB26-0587-4C30-8999-92F81FD0307C",
theme_style_1_accent_1: "3C2FFA5D-87B4-456A-9821-1D502468CF0F",
theme_style_1_accent_2: "284E427A-3D55-4303-BF80-6455036E1DE7",
theme_style_1_accent_3: "69C7853C-536D-4A76-A0AE-DD22124D55A5",
theme_style_1_accent_4: "775DCB02-9BB8-47FD-8907-85C794F793BA",
theme_style_1_accent_5: "35758FB7-9AC5-4552-8A53-C91805E547FA",
theme_style_1_accent_6: "08FB837D-C827-4EFA-A057-4D05807E0F7C",
no_style_table_grid: "5940675A-B579-460E-94D1-54222C63F5DA",
theme_style_2_accent_1: "D113A9D2-9D6B-4929-AA2D-F23B5EE8CBE7",
theme_style_2_accent_2: "18603FDC-E32A-4AB5-989C-0864C3EAD2B8",
theme_style_2_accent_3: "306799F8-075E-4A3A-A7F6-7FBC6576F1A4",
theme_style_2_accent_4: "E269D01E-BC32-4049-B463-5C60D7B0CCD2",
theme_style_2_accent_5: "327F97BB-C833-4FB7-BDE5-3F7075034690",
theme_style_2_accent_6: "638B1855-1B75-4FBE-930C-398BA8C253C6",
light_style_1: "9D7B26C5-4107-4FEC-AEDC-1716B250A1EF",
light_style_1_accent_1: "3B4B98B0-60AC-42C2-AFA5-B58CD77FA1E5",
light_style_1_accent_2: "0E3FDE45-AF77-4B5C-9715-49D594BDF05E",
light_style_1_accent_3: "C083E6E3-FA7D-4D7B-A595-EF9225AFEA82",
light_style_1_accent_4: "D27102A9-8310-4765-A935-A1911B00CA55",
light_style_1_accent_5: "5FD0F851-EC5A-4D38-B0AD-8093EC10F338",
light_style_1_accent_6: "68D230F3-CF80-4859-8CE7-A43EE81993B5",
light_style_2: "7E9639D4-E3E2-4D34-9284-5A2195B3D0D7",
light_style_2_accent_1: "69012ECD-51FC-41F1-AA8D-1B2483CD663E",
light_style_2_accent_2: "72833802-FEF1-4C79-8D5D-14CF1EAF98D9",
light_style_2_accent_3: "F2DE63D5-997A-4646-A377-4702673A728D",
light_style_2_accent_4: "17292A2E-F333-43FB-9621-5CBBE7FDCDCB",
light_style_2_accent_5: "5A111915-BE36-4E01-A7E5-04B1672EAD32",
light_style_2_accent_6: "912C8C85-51F0-491E-9774-3900AFEF0FD7",
light_style_3: "616DA210-FB5B-4158-B5E0-FEB733F419BA",
light_style_3_accent_1: "BC89EF96-8CEA-46FF-86C4-4CE0E7609802",
light_style_3_accent_2: "5DA37D80-6434-44D0-A028-1B22A696006F",
light_style_3_accent_3: "8799B23B-EC83-4686-B30A-512413B5E67A",
light_style_3_accent_4: "ED083AE6-46FA-4A59-8FB0-9F97EB10719F",
light_style_3_accent_5: "BDBED569-4797-4DF1-A0F4-6AAB3CD982D8",
light_style_3_accent_6: "E8B1032C-EA38-4F05-BA0D-38AFFFC7BED3",
medium_style_1: "793D81CF-94F2-401A-BA57-92F5A7B2D0C5",
medium_style_1_accent_1: "B301B821-A1FF-4177-AEE7-76D212191A09",
medium_style_1_accent_2: "9DCAF9ED-07DC-4A11-8D7F-57B35C25682E",
medium_style_1_accent_3: "1FECB4D8-DB02-4DC6-A0A2-4F2EBAE1DC90",
medium_style_1_accent_4: "1E171933-4619-4E11-9A3F-F7608DF75F80",
medium_style_1_accent_5: "FABFCF23-3B69-468F-B69F-88F6DE6A72F2",
medium_style_1_accent_6: "10A1B5D5-9B99-4C35-A422-299274C87663",
medium_style_2: "073A0DAA-6AF3-43AB-8588-CEC1D06C72B9",
medium_style_2_accent_1: "5C22544A-7EE6-4342-B048-85BDC9FD1C3A",
medium_style_2_accent_2: "21E4AEA4-8DFA-4A89-87EB-49C32662AFE0",
medium_style_2_accent_3: "F5AB1C69-6EDB-4FF4-983F-18BD219EF322",
medium_style_2_accent_4: "00A15C55-8517-42AA-B614-E9B94910E393",
medium_style_2_accent_5: "7DF18680-E054-41AD-8BC1-D1AEF772440D",
medium_style_2_accent_6: "93296810-A885-4BE3-A3E7-6D5BEEA58F35",
medium_style_3: "8EC20E35-A176-4012-BC5E-935CFFF8708E",
medium_style_3_accent_1: "6E25E649-3F16-4E02-A733-19D2CDBF48F0",
medium_style_3_accent_2: "85BE263C-DBD7-4A20-BB59-AAB30ACAA65A",
medium_style_3_accent_3: "EB344D84-9AFB-497E-A393-DC336BA19D2E",
medium_style_3_accent_4: "EB9631B5-78F2-41C9-869B-9F39066F8104",
medium_style_3_accent_5: "74C1A8A3-306A-4EB7-A6B1-4F7E0EB9C5D6",
medium_style_3_accent_6: "2A488322-F2BA-4B5B-9748-0D474271808F",
medium_style_4: "D7AC3CCA-C797-4891-BE02-D94E43425B78",
medium_style_4_accent_1: "69CF1AB2-1976-4502-BF36-3FF5EA218861",
medium_style_4_accent_2: "8A107856-5554-42FB-B03E-39F5DBC370BA",
medium_style_4_accent_3: "0505E3EF-67EA-436B-97B2-0124C06EBD24",
medium_style_4_accent_4: "C4B1156A-380E-4F78-BDF5-A606A8083BF9",
medium_style_4_accent_5: "22838BEF-8BB2-4498-84A7-C5851F593DF1",
medium_style_4_accent_6: "16D9F66E-5EB9-4882-86FB-DCBF35E3C3E4",
dark_style_1: "E8034E78-7F5D-4C2E-B375-FC64B27BC917",
dark_style_1_accent_1: "125E5076-3810-47DD-B79F-674D7AD40C01",
dark_style_1_accent_2: "37CE84F3-28C3-443E-9E96-99CF82512B78",
dark_style_1_accent_3: "D03447BB-5D67-496B-8E87-E561075AD55C",
dark_style_1_accent_4: "E929F9F4-4A8F-4326-A1B4-22849713DDAB",
dark_style_1_accent_5: "8FD4443E-F989-4FC4-A0C8-D5A2AF1F390B",
dark_style_1_accent_6: "AF606853-7671-496A-8E4F-DF71F8EC918B",
dark_style_2: "5202B0CA-FC54-4496-8BCA-5EF66A818D29",
dark_style_2_accent_1_accent_2:"0660B408-B3CF-4A94-85FC-2B1E0A45F4A2",
dark_style_2_accent_3_accent_4:"91EBBBCC-DAD2-459C-BE2E-F6DE35CF9A28",
dark_style_2_accent_5_accent_6:"46F890A9-2807-4EBB-B81D-B2AA78EC7F39"
}[name]
end
end
end
end
Binary file modified samples/pptx/sample.pptx
Binary file not shown.
1 change: 1 addition & 0 deletions spec/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@deck.add_pictorial_slide 'GIF Logo', 'samples/images/sample_gif.gif', {x: 124200, y: 3356451, cx: 2895600, cy: 1013460}
@deck.add_pictorial_slide 'SVG Logo', 'samples/images/sample_svg.svg'
@deck.add_textual_slide 'Why Android?', ['Its great!', 'Its sweet!']
@deck.add_table_slide "Table test", [["foo","bar","baz"],["1","2","3"]]
@deck.save 'samples/pptx/sample.pptx' # Examine the PPTX file
end

Expand Down
76 changes: 76 additions & 0 deletions template/ppt/slides/slide3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>
<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<p:cSld>
<p:spTree>
<p:nvGrpSpPr>
<p:cNvPr id="1" name="" />
<p:cNvGrpSpPr />
<p:nvPr />
</p:nvGrpSpPr>
<p:grpSpPr>
<a:xfrm>
<a:off x="0" y="0" />
<a:ext cx="0" cy="0" />
<a:chOff x="0" y="0" />
<a:chExt cx="0" cy="0" />
</a:xfrm>
</p:grpSpPr>
<p:sp>
<p:nvSpPr>
<p:cNvPr id="2" name="Title 1" />
<p:cNvSpPr>
<a:spLocks noGrp="1" />
</p:cNvSpPr>
<p:nvPr>
<p:ph type="title" />
</p:nvPr>
</p:nvSpPr>
<p:spPr />
<p:txBody>
<a:bodyPr />
<a:lstStyle />
<a:p>
<a:r>
<a:rPr lang="en-US" dirty="0" smtClean="0" />
<a:t>TITLE_PLACEHOLDER</a:t>
</a:r>
<a:endParaRPr lang="en-US" dirty="0" />
</a:p>
</p:txBody>
</p:sp>
<p:graphicFrame>
<p:nvGraphicFramePr>
<p:cNvPr id="4" name="Content Placeholder 3" />
<p:cNvGraphicFramePr>
<a:graphicFrameLocks noGrp="1" />
</p:cNvGraphicFramePr>
<p:nvPr>
<p:ph idx="1" />
<p:extLst>
<p:ext uri="{D42A27DB-BD31-4B8C-83A1-F6EECF244321}">
<p14:modId xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="2437754990" />
</p:ext>
</p:extLst>
</p:nvPr>
</p:nvGraphicFramePr>
<p:xfrm>
<a:off x="457200" y="1600200" />
<a:ext cx="8229600" cy="1010920" />
</p:xfrm>
<a:graphic>
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/table">
TABLE_PLACEHOLDER
</a:graphicData>
</a:graphic>
</p:graphicFrame>
</p:spTree>
<p:extLst>
<p:ext uri="{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}">
<p14:creationId xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main" val="1958185002" />
</p:ext>
</p:extLst>
</p:cSld>
<p:clrMapOvr>
<a:masterClrMapping />
</p:clrMapOvr>
</p:sld>