Copy code with colors using PyCharm's HTML export feature

PyCharm does not copy syntax highlighting colors to your clipboard by default — when you paste code into a document or email, the colors disappear. To keep the colors, you need to export the code as HTML first, then copy from the HTML file. The fastest method is to select your code, go to File > Export, choose HTML, and paste the resulting colored code into any process that accepts rich text (like Google Docs, Word, or Slack).

If you need colors in a plain-text tool like email or a code repository, you have a second option: take a screenshot of the code window instead. PyCharm's built-in screenshot tool captures the colors exactly as they appear on screen. This works everywhere but creates an image rather than selectable text.

Key Takeaways

  • PyCharm's standard copy function strips syntax colors; you must export to HTML to preserve them.
  • The HTML export method works best for documents, slides, and messaging apps that support rich text formatting.
  • Screenshots preserve colors and appearance but create an image file instead of text you can edit or search.
  • Copy-paste from the HTML file works in Google Docs, Microsoft Word, Slack, and most modern applications.
  • The colors in the exported HTML match your current PyCharm theme, so changing your theme changes the exported colors.

Export code as HTML in three steps

Open the file containing the code you want to copy. Select the lines you need by clicking at the start, holding Shift, and clicking at the end. If you want the whole file, press Ctrl+A (or Cmd+A on Mac).

Go to the menu bar and click File > Export. A dialog box opens. Choose HTML from the list of export formats. PyCharm will ask you where to save the file — pick a location you can find easily, like your Desktop. Click Export.

Open the HTML file in your web browser. The code appears with full syntax highlighting in the colors of your PyCharm theme. Select all the colored code (Ctrl+A or Cmd+A), copy it (Ctrl+C or Cmd+C), and paste it into your destination — a Google Doc, Slack message, email, or any other rich-text process. The colors come with it.

When to use screenshots instead of HTML export

Screenshots work better when you need to share code in a format that does not support pasting formatted text. If you are posting to a platform that only accepts images, or if you want to show the exact line numbers and error messages from PyCharm's interface, a screenshot is faster than exporting.

PyCharm has a built-in screenshot tool. Press Ctrl+Shift+X (or Cmd+Shift+X on Mac) to open the screenshot dialog. Click and drag to select the area of the code you want to capture. PyCharm saves the image to your clipboard when ready, and you can paste it anywhere. The image includes the code, the line numbers, and the syntax colors exactly as they appear in your editor.

The downside is that screenshots create static images — someone reading your code cannot copy text from it, search within it, or modify it. Use screenshots when appearance and context matter more than editability.

Choosing the right theme for exported colors

The colors in your exported HTML depend on which PyCharm theme you have active. PyCharm comes with several built-in themes: Darcula (dark background), IntelliJ Light (light background), and others. If you export code while using Darcula, the HTML will have a dark background and light text. If you switch to IntelliJ Light and export the same code, the HTML will have a light background and dark text.

Before exporting, check which theme you are using. Go to File > Settings (or PyCharm > Preferences on Mac) > Appearance & Behavior > Appearance. The current theme is shown in the Theme dropdown. If you want different colors in your export, switch themes here, then export again.

This matters because a dark-background export looks good in a dark-themed document or presentation, but may be hard to read if you paste it into a light-background email. Choose your theme based on where the code will end up.

Pasting colored code into common applications

Google Docs: Open the HTML file in your browser, select and copy the colored code, then paste it directly into your Google Doc. The colors and formatting transfer automatically. Google Docs preserves the syntax highlighting as styled text.

Microsoft Word: Copy from the HTML file and paste into Word. Word will ask whether you want to keep the source formatting or match the destination formatting. Choose "Keep Source Formatting" to preserve the colors. If the colors do not appear, right-click the pasted text and select "Keep Source Formatting" from the context menu.

Slack: Slack does not support pasting formatted text with colors in the message box. Instead, paste the code as plain text, then use Slack's code block feature (triple backticks) to add syntax highlighting. Alternatively, upload a screenshot or the HTML file itself as a file attachment.

Email: Most email clients (Gmail, Outlook, Apple Mail) support rich text. Copy from the HTML file and paste into the email body. The colors usually transfer, but some email clients strip formatting. If colors disappear, try pasting as "Paste Special" > "Unformatted Text" and manually reapply colors, or send a screenshot instead.

Troubleshooting: colors not appearing after paste

If you paste the code and the colors do not show up, the destination process may not support rich text formatting. Check whether the process has a "Rich Text" or "Format" mode. In Google Docs, this is automatic. In Word, make sure you are not in "Plain Text" mode. In some email clients, you need to switch from "Plain Text" to "HTML" or "Rich Text" mode before pasting.

Another common issue is that the destination process strips inline styles. If this happens, try pasting the code into a temporary Google Doc first (which preserves formatting), then copying from the Doc into your final destination. Some applications accept formatted text from Google Docs when they would reject it from other sources.

If you are exporting to HTML and the file opens in your browser with no colors at all, check that you selected the code before exporting. If you exported the whole file by accident, the HTML may include code you did not intend. Delete the HTML file, select only the code you need, and export again.

Frequently Asked Questions

Can I copy code with colors directly without exporting to HTML?

No. PyCharm's standard copy function (Ctrl+C) copies only the text, not the formatting. You must export to HTML first, then copy from the HTML file in your browser. There is no setting in PyCharm to change this behavior.

Does the exported HTML file include the entire PyCharm interface or just the code?

Just the code. The HTML export creates a standalone file with only the selected code, formatted with syntax highlighting. It does not include line numbers, error messages, or the editor interface unless you specifically select those elements before exporting.

What if I want to keep the line numbers when I copy the code?

Select the line numbers along with the code before exporting. Click in the line number column on the left, hold Shift, and click at the end of the code you want. When you export, the line numbers will be included in the HTML. Alternatively, take a screenshot instead — screenshots always include line numbers.

Will the colors look the same in every process I paste into?

Mostly, but not always. The colors depend on how the destination process renders HTML and styled text. Google Docs and Word usually preserve colors accurately. Email clients and some messaging apps may adjust colors to match their own background or strip formatting entirely. If colors matter, test in your destination process first.

Can I export code with a different theme than the one I am currently using?

Yes. Switch to the theme you want in Settings > Appearance > Appearance, then export. PyCharm will export using the active theme. You do not need to restart PyCharm for the change to take effect.