Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Almost any task that you want to perform with Aspose.Words involves loading a document. The Document class represents a document loaded into memory. The document has several overloaded constructors allowing you to create a blank document or to load it from a file or stream. The document can be loaded in any load format supported by Aspose.Words. For the list of all supported load formats, see the LoadFormat enumeration.
We will call the Document constructor without parameters to create a new blank document. If you want to generate a document programmatically, the simplest way is to use the DocumentBuilder class to add document contents.
The following code example shows how to create a document using the document builder:
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.write("Hello world!")
doc.save(docs_base.artifacts_dir + "out.docx")Note the default values:
To load an existing document in any of the LoadFormat formats, pass the file name or the stream into one of the Document constructors. The format of the loaded document is automatically determined by its extension.
Pass a file name as string to the Document constructor to open an existing document from a file.
The following code example shows how to open a document from a file:
You can download the template file of this example from Aspose.Words GitHub.
To open a document from a stream, simply pass a stream object that contains the document into the Document constructor.
The following code example shows how to open a document from a stream:
Q: How do I create a completely blank Word document?
A: Instantiate the Document class without any arguments. The resulting document contains a single empty section and paragraph, which matches the “New document” created by Microsoft Word. Example:
doc = aw.Document()
Q: How can I load an existing document from a file path?
A: Pass the full file name (including extension) to the Document constructor. Aspose.Words automatically detects the format based on the extension. Example:
doc = aw.Document("C:/Docs/Report.docx")
Q: What is the correct way to load a document from a memory stream?
A: Create a stream (e.g., io.FileIO) that contains the document bytes and give that stream to the Document constructor. Example:
stream = io.FileIO(docs_base.my_dir + "Document.docx")
doc = aw.Document(stream)
stream.close()
Q: Can I use a .NET license file (e.g., Aspose.Total.NET.lic) with Aspose.Words for Python via .NET?
A: Absolutely. The Python API runs on top of the .NET library, so you can load a .NET license file in the same way as in a C# project. Example:
license = aw.License()
license.set_license("C:/Licenses/Aspose.Total.NET.lic")
After setting the license, all subsequent operations will be licensed for the Python environment.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.