Python 将代码复制到word文档中并保持格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22681832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:24:25  来源:igfitidea点击:

Copying code into word document and keeping formatting

pythonms-word

提问by user2996828

I need to get my code (Python 2.7 written in the Python IDE) into a word document for my dissertation but I am struggling to find a way of copying it in and keeping the formatting, I've tried paste special and had no luck. The only way I've found so far is screenshoting but with just over 1000 lines of code this is proving very laborious.

我需要将我的代码(在 Python IDE 中编写的 Python 2.7)放入我的论文的 word 文档中,但我正在努力寻找一种方法来复制它并保持格式,我尝试过特殊粘贴但没有成功。到目前为止,我发现的唯一方法是截图,但只有 1000 多行代码,这证明非常费力。

采纳答案by user2996828

After trying every method I still had problems then came across this!, basically copy and paste your code and select what language it is (e.g. python, java, ...), and it will kick out the formatted and coloured version ready to be pasted into word :-)

在尝试了每种方法后,我仍然遇到了问题,然后遇到了这个问题!,基本上是复制并粘贴您的代码并选择它是什么语言(例如 python、java、...),它会踢出准备好的格式化和彩色版本粘贴到单词中 :-)

Code to Word and keep formatting

编码到 Word 并保持格式

回答by user3441996

To fix this, instead of directly copying and pasting, use Insert -> Object -> OpenDocument Text. The second option is to create a style for your code.

要解决此问题,请使用插入 -> 对象 -> OpenDocument 文本,而不是直接复制和粘贴。第二个选项是为您的代码创建样式。