Skip to content

Copy RTF Text

Pygments provides a way of exporting RTF, but copying that and pasting it into Office apps doesn't work, as it will end up as regular text.

rtfcopy.ps1 can copy the text as RTF and pasting that text into for example PowerPoint will result in colorized text.

Usage: pygmentize -f rtf <file> | rtfcopy.ps1.

bin/windows/rtfcopy.ps1
Add-Type -AssemblyName System.Windows.Forms;

$text = $input -join '`r`n';

$data = New-Object System.Windows.Forms.DataObject;
$data.SetData([System.Windows.Forms.DataFormats]::Text, $text);
$data.SetData([System.Windows.Forms.DataFormats]::Rtf, $text);
[System.Windows.Forms.Clipboard]::SetDataObject($data);

Write-Host "Copied!" -ForegroundColor Green;

Source