Skip to content

Conversation

@athompson673
Copy link
Contributor

@athompson673 athompson673 commented Jan 10, 2026

Description of Changes

Added the ability to copy as html in order to include syntax formatting (text color, bold, and italic).

Media1.mp4
  • Wrote at least one-line docstrings (for any new functions)
  • Added unit test(s) covering the changes (if testable)
  • Included a screenshot or animation (if affecting the UI, see Licecap)
  • Wrote a simple html emitter as the built-in .toHtml methods (on QTextCursor.selection() and QTextDocument) do not seem to capture formatting applied by a QSyntaxHighlighter.
  • Wrote a simple rtf emitter to export files as rtf with styling.
  • Added html to regular copy function so copied text can be pasted into rich text editors with syntax highlighting.
  • Added file menu actions for exporting the current file to either html or rtf.

Issue(s) Resolved

Fixes #25534

Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the Developer Certificate of Origin
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

I certify the above statement is true and correct: athompson673

@athompson673
Copy link
Contributor Author

athompson673 commented Jan 12, 2026

@ccordoba12 I see that all the conda tests are failing due to the new requirement. (also 3.9 tests are failing on type annotation that I forgot was 3.10+) Is there some way to pull the pip package into conda? It's also a really simple stdlib-only single-file library with a permissive license if we want to inline it.

@ccordoba12
Copy link
Member

I see that all the conda tests are failing due to the new requirement.

Yep, that's because rtfunicode is not available in Conda-forge, so we'd need to add it. If it's a pure Python package, it should be relatively simple.

Instructions for that are available in this repo. But let me know if you don't have much experience with packaging and I'll give you a hand with that.

also 3.9 tests are failing on type annotation that I forgot was 3.10+

I took a look at your last commit and I think you only need to add from __future__ import annotations as the first import in the file you added annotations to. Please try add by amending your previous commit (although our code is not heavily annotated, we try to do it for new code).

@ccordoba12 ccordoba12 added this to the v6.2.0 milestone Jan 13, 2026
@athompson673 athompson673 changed the title PR: Add the ability to copy text with syntax highlighting via text/html mime type PR: Add the ability to copy text and export files with syntax highlighting. Jan 13, 2026
@athompson673
Copy link
Contributor Author

I see that all the conda tests are failing due to the new requirement.

Yep, that's because rtfunicode is not available in Conda-forge, so we'd need to add it. If it's a pure Python package, it should be relatively simple.
Instructions for that are available in this repo. But let me know if you don't have much experience with packaging and I'll give you a hand with that.

I have never really done any packaging at all. I'm sure I could give it a look and figure it out, but I wouldn't mind the help either.

also 3.9 tests are failing on type annotation that I forgot was 3.10+

I took a look at your last commit and I think you only need to add from __future__ import annotations as the first import in the file you added annotations to. Please try add by amending your previous commit (although our code is not heavily annotated, we try to do it for new code).

Yah, I forgot I could do that.. I'll add them back in.

somehow initially started using an undocumented property which evidently doesn't work on some systems. change to intended property.
@ccordoba12
Copy link
Member

I have never really done any packaging at all. I'm sure I could give it a look and figure it out, but I wouldn't mind the help either.

I took a quick look at the package and I see two problems with it:

  • Last time it was updated was six years ago, so it could be a problem if a future Python version breaks it.
  • It doesn't have wheels on PyPI, which is a problem for projects like Winpython.

So, could you replace it with a better maintained dependency that allows us to support RTF?

Copy link
Member

@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested both the copy/paste and the export functionality in both formats on Windows and with LibreOffice, plain text editors and various applications, and everything seemed to mostly work well. With that in mind, it seems there isn't a strong disadvantage to including the HTML version in the clipboard for applications that want it that would justify the complexity of a separate action, menu item, keyboard shortcut, etc. as the plain text version still seems to work fine with plain text applications as before.

One thing, though--it seems like it uses the dark theme colors but a light theme background, rendering the text low contrast and in some cases (especially non-specially-highlighted text) almost unreadable. I'm not sure how feasible it is with the existing theme infra and/or with @conradolandia 's upcoming changes, but it would be great if we could use the light version of the current theme if available (not sure whether to fall back to the current theme or the Spyder light theme if the current theme doesn't have a light version, as most of them don't besides the default and a few popular ones).

image

Also in RTF, indentation was double what it should be (8 spaces instead of 4 for each indent); for HTML it was correct.

Finally, see the suggestions for a couple other comments and various minor typo, UI text, docstring and formatting/convention fixes. As a reminder, you can apply them all in one go by navigating to the Files tab, clicking Add to batch on each suggestion, and then once they are all added, clicking the Commit button at the top right.

@CAM-Gerlach CAM-Gerlach changed the title PR: Add the ability to copy text and export files with syntax highlighting. PR: Add the ability to copy text and export files with syntax highlighting Jan 15, 2026
@ccordoba12
Copy link
Member

One thing, though--it seems like it uses the dark theme colors but a light theme background, rendering the text low contrast

Good point. I think this is something we should address before merging this PR.

@mjpieters
Copy link

I took a quick look at the package and I see two problems with it:

  • Last time it was updated was six years ago, so it could be a problem if a future Python version breaks it.

  • It doesn't have wheels on PyPI, which is a problem for projects like Winpython.

Hi, I'm the author of said package. The only reason it hasn't seen updates in all these years is because I wasn't sure if anyone was still using it!

I'd be happy to update it for modern use, complete with wheel releases. Give me a day or two to modernise the tooling and publish a new release tested against current Python releases. Would that help?

@ccordoba12
Copy link
Member

I'd be happy to update it for modern use, complete with wheel releases. Give me a day or two to modernise the tooling and publish a new release tested against current Python releases. Would that help?

Sure, thanks a lot for your help @mjpieters!

@athompson673
Copy link
Contributor Author

athompson673 commented Jan 15, 2026

One thing, though--it seems like it uses the dark theme colors but a light theme background, rendering the text low contrast

Good point. I think this is something we should address before merging this PR.

I'm currently just pulling the actual formatting from each QTextBlock, so it will follow the current style. I still haven't found a way to set the background color for an RTF file, so maybe similar to how printing works where a temporary hidden editor is created with a fixed (light) style to copy from?

edit: I found a possible solution that at least seems to work on MS Word: for background color \shading\cbpatN

On the other hand; for copying html, it seems adding style = "background-color ..." to the <pre> tag works at least in MS word (putting it in other places like <body> and <span> wasn't quite right).

image

I do like the idea of copying exactly the style on screen if possible, but maybe for RTF specifically we just resort to a default light theme?

@mjpieters Thanks again for giving this a look!

@athompson673
Copy link
Contributor Author

@CAM-Gerlach

Also in RTF, indentation was double what it should be (8 spaces instead of 4 for each indent);

I'm not seeing this behavior. Do you use tab indent for source code? What program are you viewing the RTF with? I'm using notepad++ to check the plaintext of the rtf generated. You should see for each line of text a command sequence which terminates with a space at the beginning of each line, then an identical number of spaces as the source from which it was exported.

image

@mjpieters
Copy link

mjpieters commented Jan 16, 2026

I'd be happy to update it for modern use, complete with wheel releases. Give me a day or two to modernise the tooling and publish a new release tested against current Python releases. Would that help?

Sure, thanks a lot for your help @mjpieters!

I released rtfunicode 2.1, compatible with Python 3.10 and up*, complete with a wheel. As a bonus, the code is now also type-annotated.


*) I had to bump the minimal supported version so I could also run Ruff and Pyright; 3.10 is the oldest still-supported Python version. Happy to extend this down if that's still needed here.

@athompson673
Copy link
Contributor Author

*) I had to bump the minimal supported version so I could also run Ruff and Pyright; 3.10 is the oldest still-supported Python version. Happy to extend this down if that's still needed here.

Spyder still tests against 3.9, and I actually ran up against that in this PR earlier trying to use annotations with typeA | typeB.

@mjpieters
Copy link

Spyder still tests against 3.9, and I actually ran up against that in this PR earlier trying to use annotations with typeA | typeB.

I just released rtfunicode version 2.2, which lowered the minimum Python version to 3.9. I'll raise it again for 2.3 however, as keeping 3.9 requires conditional dev dependencies and that all complicates maintenaince. So if you pin your depenency on rtfunicode>=2.2,<3 you'll get either 2.2 or 2.3 depending on what Python version you are installing on.

@athompson673 athompson673 marked this pull request as ready for review January 28, 2026 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add “Export to RTF” option for code editor with syntax highlighting

4 participants