2020-07

    技术2025-08-27  15

    python-docx 0.8.10 User Guide

    说明:

    本文的内容来源python-docx官方文档,出于学习的目的借助于有道进行翻译。翻译的原则:只有在有道翻译出的内容不通顺或意义有误的情况下才使用个人翻译。

    API basics

    The API for python-docx is designed to make doing simple things simple, while allowing more complex results to be achieved with a modest and incremental investment of understanding. python-docx的API旨在使简单的事情变得简单,同时允许通过适度和增量的理解投资来实现更复杂的结果。

    It’s possible to create a basic document using only a single object, the docx.api.Document object returned when opening a file. The methods on docx.api.Document allow block-level objects to be added to the end of the document. Block-level objects include paragraphs, inline pictures, and tables. Headings, bullets, and numbered lists are simply paragraphs with a particular style applied. 只使用一个对象(docx.api)就可以创建一个基本文档。打开文件时返回的文档对象。api上的方法。文档允许块级对象添加到文档的末尾。块级对象包括段落、内联图片和表。标题、项目符号,而编号列表只是应用了特定样式的段落。

    In this way, a document can be “written” from top to bottom, roughly like a person would if they knew exactly what they wanted to say This basic use case, where content is always added to the end of the document, is expected to account for perhaps 80% of actual use cases, so it’s a priority to make it as simple as possible without compromising the power of the overall API. 通过这种方式,一份文件可以从上到下“书写”,就像一个人知道自己想说什么就会写什么一样。这个基本用例(内容总是被添加到文档的末尾)预计将占实际用例的80%,因此在不影响整个API功能的情况下,使其尽可能简单是一个优先事项。

    Inline objects Each block-level method on docx.api.Document, such as add_paragraph(), returns the block-level object created. Often the reference is unneeded; but when inline objects must be created individually, you’ll need the block-item reference to do it. api上的每个块级方法。文档,比如add_段落(),返回创建的块级对象。通常引用是不必要的;但是,当必须单独创建内联对象时,需要使用块项引用来完成。

    … add example here as API solidifies … 在这里添加示例作为API的巩固…

    Processed: 0.011, SQL: 10