Skip to content

Instantly share code, notes, and snippets.

@DanChaltiel
Created December 30, 2019 10:59
Show Gist options
  • Select an option

  • Save DanChaltiel/e7505e62341093cfdc489265963b6c8f to your computer and use it in GitHub Desktop.

Select an option

Save DanChaltiel/e7505e62341093cfdc489265963b6c8f to your computer and use it in GitHub Desktop.
An RMarkdown pandoc filter for MS Word docx output
-- Extension ofTarleb's answer on Stackoverflow (https://stackoverflow.com/a/52131435/3888000) to include docx section ends with portrait/landscape orientation changes.
-- Also uses officer package syntax to create sections breaks
local function newpage(format)
if format == 'docx' then
local pagebreak = '<w:p><w:r><w:br w:type="page"/></w:r></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
else
return pandoc.Para{pandoc.Str '\f'}
end
end
local function endLandscape(format)
if format == 'docx' then
local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:pPr><w:sectPr><w:officersection/><w:pPr><w:sectPr><w:officersection/><w:pgSz w:orient=\"landscape\" w:w=\"11906\" w:h=\"16838\"/></w:sectPr></w:pPr></w:sectPr></w:pPr></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
else
return pandoc.Para{pandoc.Str '\f'}
end
end
local function endPortrait(format)
if format == 'docx' then
local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:pPr><w:sectPr><w:officersection/><w:pPr><w:sectPr><w:officersection/><w:pgSz w:orient=\"portrait\" w:w=\"16838\" w:h=\"11906\"/></w:sectPr></w:pPr></w:sectPr></w:pPr></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
else
return pandoc.Para{pandoc.Str '\f'}
end
end
local function endContinuous(format)
if format == 'docx' then
local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:pPr><w:sectPr><w:officersection/><w:type w:val=\"continuous\"/></w:sectPr></w:pPr></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
else
return pandoc.Para{pandoc.Str '\f'}
end
end
-- Filter function called on each RawBlock element.
function RawBlock (el)
if el.text:match '\\newpage' then
return newpage(FORMAT)
elseif el.text:match '\\endLandscape' then
return endLandscape(FORMAT)
elseif el.text:match '\\endContinuous' then
return endContinuous(FORMAT)
elseif el.text:match '\\endPortrait' then
return endPortrait(FORMAT)
end
-- otherwise, leave the block unchanged
return nil
end
@gubin-i-da
Copy link

I have the same problem - after switching from landscape to portrait orientation, an extra page break is added.
There is another unpleasant feature.
If a portrait-oriented document has templates for the title and regular pages, then the following behavior is observed: the transition from portrait to landscape occurs normally and picks up the headers and footers from the template for regular pages, however, the subsequent transition from landscape to portrait orientation is performed strangely - the page gets the header and footer not for the regular page but for the title page.
Any ideas on how to fix this behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment