Copy code with colors using PyCharm's built-in export feature
PyCharm does not copy syntax highlighting to your clipboard by default — when you paste code elsewhere, it arrives as plain text. To preserve colors, you need to use PyCharm's Copy as Rich Text feature or export the code as an HTML file. The simplest method is right-clicking your selection and choosing Copy as Rich Text, which works in most applications that accept formatted text, including Microsoft Word, Google Docs, and Slack. If that option does not appear, you can export the entire file or selection as HTML, which preserves all colors and formatting.
The method you choose depends on where you are pasting the code. Rich text copying works best for documents and messaging apps. HTML export works best when you need a standalone file or are pasting into a web page or blog.
Key Takeaways
- Right-click your code selection and choose Copy as Rich Text to copy colors to Word, Google Docs, Slack, and similar applications.
- If Copy as Rich Text does not appear, go to Edit > Copy as Rich Text from the menu bar instead.
- Export code as HTML by selecting your code, then going to File > Export > Export to HTML to create a file with full formatting.
- Some applications like plain text editors and terminals cannot receive formatted text, so pasting will show plain text regardless of your copy method.
Using Copy as Rich Text from the right-click menu
The fastest way to copy colored code is through the context menu. Highlight the code you want to copy, right-click the selection, and look for Copy as Rich Text. Click it, then paste into your destination application using Ctrl+V (Windows) or Cmd+V (Mac). The colors, font style, and indentation should transfer.
This method works in Google Docs, Microsoft Word, Notion, Slack, Discord, and most other applications that support formatted text. It does not work in plain text editors like Notepad or in terminal windows, which strip formatting automatically.
If you do not see Copy as Rich Text in the right-click menu, your selection might be too large or PyCharm might not recognize the file type. Try selecting a smaller block of code, or use the menu bar method instead.
Accessing Copy as Rich Text from the menu bar
If the right-click option does not appear, you can reach the same feature through PyCharm's menu. Select the code you want to copy, then click Edit in the menu bar. Scroll down to find Copy as Rich Text and click it. Your code is now copied with colors intact.
This method is also useful if you prefer using keyboard shortcuts. Some PyCharm configurations allow you to assign a custom keyboard shortcut to Copy as Rich Text. Go to File > Settings (Windows) or PyCharm > Preferences (Mac), then search for "Copy as Rich Text" under Keymap to set your own shortcut.
Exporting code as an HTML file
When you need a standalone file with full formatting, export your code as HTML. Select the code or open the file you want to export, then go to File > Export > Export to HTML. PyCharm creates an HTML file that displays your code with all colors, fonts, and line numbers preserved. You can open this file in any web browser or share it with others.
The HTML export includes a complete styled page, so the file is larger than the original code file. This method is best when you are creating documentation, sharing code snippets on a website, or building a code portfolio. You can also edit the HTML file afterward to adjust colors or add explanations.
Why colors do not copy by default
PyCharm's standard copy function (Ctrl+C) copies only the text itself, not the formatting. This is because most applications expect plain text on the clipboard. Syntax highlighting is stored separately in PyCharm's display layer, not in the text data itself. Copy as Rich Text converts the highlighted code into a format that other applications can read and display — usually RTF (Rich Text Format) or HTML.
Different applications handle rich text differently. Google Docs and Word preserve all colors and styles. Slack and Discord preserve colors but may adjust fonts. Plain text destinations like email plain-text mode or terminal windows ignore formatting entirely and show only the text.
Choosing the right color scheme for copying
The colors that copy depend on your current PyCharm theme. If you use the default dark theme, your copied code will have a dark background. If you use a light theme, the background will be light. Before copying, check that your theme matches where you are pasting — dark code on a dark document background becomes unreadable.
You can change your theme temporarily by going to File > Settings (Windows) or PyCharm > Preferences (Mac), then Appearance & Behavior > Appearance. Choose a theme that works for your destination, copy your code, then switch back. Alternatively, some applications like Google Docs let you paste the code and then adjust the background color after pasting.
Troubleshooting when colors do not transfer
If your code pastes as plain text even after using Copy as Rich Text, the destination application may not support formatted text. Email clients in plain-text mode, terminal windows, and basic text editors cannot display colors. Try pasting into a different application to test — Google Docs is a reliable test because it accepts rich text from almost any source.
If colors work in some applications but not others, check that application's settings. Some messaging apps have a "plain text mode" that strips formatting. Some document editors require you to paste as "formatted text" rather than "paste special." If you consistently need colors in plain-text environments, HTML export is your best option — you can open the HTML file in a browser and screenshot it, or share the HTML file itself.
Frequently Asked Questions
Does Copy as Rich Text work on Mac and Windows?
Yes, Copy as Rich Text is available on both Mac and Windows versions of PyCharm. The menu location and keyboard shortcuts are the same. Paste using Cmd+V on Mac or Ctrl+V on Windows.
Can I copy code with line numbers?
When you copy code normally, line numbers do not copy. If you need line numbers, use the HTML export method — it includes them in the exported file. You can then take a screenshot or copy from the HTML file in your browser.
What if I want to change the colors before copying?
Switch to a different PyCharm theme before copying. Go to File > Settings > Appearance & Behavior > Appearance and choose a new theme. Copy your code, then switch back to your preferred theme.
Does Copy as Rich Text preserve indentation and spacing?
Yes, indentation and spacing transfer with the colors. The formatting stays intact in applications that support rich text, like Word and Google Docs.
Can I copy code from PyCharm into a PowerPoint presentation with colors?
Yes, use Copy as Rich Text and paste directly into PowerPoint. The colors and formatting will appear. If colors look different, check that your PowerPoint background color contrasts with your code colors.