在Word中自动缩进包装Python代码

发布于 2024-11-02 21:09:24 字数 1366 浏览 1 评论 0原文

我正在努力使用 Microsoft Word 来正确显示我的 Python 代码,并且需要一些帮助。

我正在尝试将大量 Python 脚本粘贴到 Microsoft Word 中,并在片段周围编写文档文本。其中一些 Python 片段只有几行,其他的则长达一页以上。由于该文档现在大约有 500 页长,因此大约有 200 个片段块分散在整个文档中。

我将我创建的字体样式设置为片段。所以我可以一次更改所有这些的字体大小、颜色、样式等。但我在自动换行方面遇到了很大的问题。 Python 中的长语句会被包裹在单词中,这使得它们难以阅读,因为缩进丢失了。我能够使用“悬挂缩进”成功缩进 1 级换行。但我无法对 2 级或 3 级缩进做任何事情,因为嵌套的内容会进一步缩进。

示例(我使用点而不是空格,因为它不断删除它们)

This is a statement
This is another statement
if (condition):
.........This is a third statement
.........This is a fourth statement
.........for loop : 
..................This is a fifth statement
..................This is a sixth statement
..................if (condition):
...........................This is a seventh statement

想象每个语句相当长并且被换行到单词页面上的下一行。我明白了

This is a statement
This is another statement
if (condition):
.........This is a third     
statement
.........This is a fourth     
statement
.........for loop:
.................This is
a fifth statement
.................This is a
sixth statement 

如何在word中解决这个问题?悬挂缩进将修复 1 级缩进(if 条件中的语句),但不会修复 2 级缩进(for 循环中的语句)

注意:我想在 word 中使用某种选项、插件或宏。我无法使用代码编辑器以 rtf 或其他格式复制和粘贴代码。即使我这样做了 200 次来替换所有代码片段,当我更改文档中的字体大小时,一切都会再次变得混乱。另一种选择是某种 IDE,它嵌入或链接到 Word(无需复制和粘贴),并允许在其自己的环境中更改字体样式和大小,这些更改将在 Word 中的所有出现位置自动更新。

如果可以的话请帮忙。我疯狂地搜索,但没有发现任何有用的东西......

I'm wrestling with Microsoft Word to display my Python code correctly and am in need of some help.

I am trying to paste large amounts of Python scripts into Microsoft Word with documentation text written around the snippets. Some of these Python snippets are a few lines, others are over a page long. Since the document is now around 500 pages long there are around 200 snippet blocks scattered throughout the document.

I have a font style I created set to the snippets. So I can change font size, color, style etc for all of them at once. But I'm having a big problem with the word wrapping. Long statements in Python get wrapped in word which makes them hard to read since the indents are lost. I am able to successfully indent the level 1 wrapped line using "hanging indents". But I cannot do anything about a level 2 or level 3 indent since nested stuff is further indented.

Example (I've used dots instead of spaces because it kept removing them)

This is a statement
This is another statement
if (condition):
.........This is a third statement
.........This is a fourth statement
.........for loop : 
..................This is a fifth statement
..................This is a sixth statement
..................if (condition):
...........................This is a seventh statement

Imagine each statement is fairly long and gets wrapped to the next line on a word page. I get

This is a statement
This is another statement
if (condition):
.........This is a third     
statement
.........This is a fourth     
statement
.........for loop:
.................This is
a fifth statement
.................This is a
sixth statement 

How can I fix this in word? A hanging indent will fix the level 1 indents (the statements in the if condition) but not the level 2 indents (the statements in the for loop)

Note: I would like to use some sort of option, or plugin or macro within word. I cannot use a code editor and copy and paste code in rtf or some other format. Even if I did this 200 times to replace all my code snippets, the moment I change the font size in my document everything will get messed up again. Another option would be some IDE that embeds or links into word (without having to copy and paste) and allows changes to font style and size in its own environment which will get updated at all occurrences in Word automatically.

Please help if you can. I have searched like crazy and found nothing that works...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜味拾荒者 2024-11-09 21:09:24

1) 遵循 PEP-8 建议并保持行 < 80 个字符。
有时这看起来非常困难或不方便。在这些情况下,最多允许包含 90-95 个字符。较长的行可能是代码设计不当或变量名称选择错误的结果。 (有人使用最多 120 个字符的标准尺寸,但他们可能并不想在 Word 中以纵向模式发布代码)。

2) 使用等宽字体

3) 保持字体大小足够小,以每行提供 80-95 个字符。

1) Follow PEP-8 recomendations and keep lines < 80 characters.
Sometimes this seems very difficult or inconvenient. In these cases allow you up to 90-95 characters. Longer lines are probably the result of a bad design of the code or of wrongly selected variable names. (There is people working with standard sizes of up to 120 characters but probably they are not trying to publish the code in Word in portrait mode).

2) Use a monospaced font

3) Keep font size small enough to provide 80-95 characters per line.

阳光①夏 2024-11-09 21:09:24

您是否尝试过使用 Word 起草纯文本文档?您以后随时可以转换它。

Have you tried using Word to draft a plain-text document? You can always convert it later.

源来凯始玺欢你 2024-11-09 21:09:24

在支持 Python 的代码编辑器中使用语法突出显示编写代码。保存您的片段。截图。将它们粘贴到 MS Word 中。根据需要调整图像大小并裁剪图像。

现在您所要做的就是在图像自动换行方面与 MS Word 进行战斗,这是一场您甚至可能获胜的战斗。

Write your code in a Python-enabled code editor with syntax highlight. Save your snippets. Take screenshots. Paste them into MS Word. Resize and crop the images as desired.

Now all you have to do is fight MS Word on the word-wrapping around images, which is a fight you might even win.

叹倦 2024-11-09 21:09:24

使用docutils

使用 docutils 比与 MS-Word(和其他所见即所得编辑器)对抗要容易得多。

  1. 以大致纯文本形式编写您的文档。您将使用非常简单且轻量级的 RST 标记。

  2. 运行 rst2html.py 转换,从源代码创建美观的 HTML 页面。

  3. 运行rst2latex.py转换以从您的源创建LaTeX。有多种工具可以从 LaTeX 生成 PDF。

在这种情况下,代码片段每次都能得到完美的处理。没有工作。

如果您正在编写非常庞大且复杂的内容,则应该使用 Sphinx 来实现。它是 docutils 的扩展,具有更酷的代码片段标记功能。

Use docutils.

Instead of fighting against MS-Word (and other WYSIWYG editors) it's far, far easier to use docutils.

  1. Write your document in approximately plain text. You'll use RST markup which is very simple and lightweight.

  2. Run the rst2html.py conversion to create nice-looking HTML pages from your source.

  3. Run the rst2latex.py conversion to create LaTeX from your source. There are a variety of tools that can produce PDF from the LaTeX.

In this case, the code snippets are handled perfectly every single time. No work.

If you're writing something really big and complex, you should be using Sphinx for this. It's an extension to docutils with even more cool markup features for code snippets.

若无相欠,怎会相见 2024-11-09 21:09:24

我不使用 word,但在 LibreOffice 中,您可以只使用段落格式 - 为每个缩进级别创建一个新的段落样式(pycodepycode_indent1...) 。将所需的所有格式(等宽、无段落间距等)放入顶级样式中,并使缩进样式将其用作父样式。然后只需为每个子样式添加适当的缩进即可。这与多深度项目符号列表基本相同,但没有项目符号。然后为每行选择适当的缩进段落样式(提示:您可以在 LibreOffice 中使用 ctrl+鼠标选择多个不连续的行)。

当然,这样你就必须逐行进行,这可能会很痛苦。但如果只是一些有问题的片段,那么可能会起作用。

I don't use word, but in LibreOffice, you could just use paragraph formatting - create a new paragraph style for each level of indent (pycode, pycode_indent1...). Put all of the formatting you want (mono-spaced, no paragraph spacing, etc.) in the top-level style, and make the indented styles use it as a parent. Then just add appropriate indents to each of the child styles. This is basically the same idea as multi-depth bulleted list, without the bullets. Then select the appropriate indent paragraph style for each line (hint: you can select multiple non-contiguous lines using ctrl+mouse in LibreOffice).

Granted, this way you have to do it line-by line, which could be a big pain in the arse. But if might work if it's just a few snippets that are being problematic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文