Skip to content

Instantly share code, notes, and snippets.

@DanChaltiel
Created December 30, 2019 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DanChaltiel/e7505e62341093cfdc489265963b6c8f to your computer and use it in GitHub Desktop.
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
@aleyner
Copy link

aleyner commented Apr 13, 2023

Thanks for your filter! It works great with my document. But there is a problem when you need to change the orientation at the very end of the document. The filter adds a page break, which results in a blank page at the end. Can this be bypassed somehow?

@DanChaltiel
Copy link
Author

I think I had this problem once and conclude this was an MS Word limitation :-(

@aleyner
Copy link

aleyner commented Apr 14, 2023

Thanks for the answer. Yes, unfortunately I haven't been able to solve the problem yet. I thought to remove it by running an auto macro in the Word itself, but so far this does not work.

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