Articles in this section
Category / Section

How to Create a Word Document using DocIO in ASP.NET Core?

4 mins read

This article explains how to use Syncfusion® DocIO to create Word documents from a Python script by wrapping a .NET class library. This approach uses pythonnet to load a compiled C# library in Python, allowing the powerful features of DocIO to be accessed from Python code.

Steps to create the C# library that uses Syncfusion® DocIO:

  1. Create a new class library project.
    Create class library application in Visual Studio
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
    Add DocIO NuGet package reference to the project
Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.

  1. Create a class file named CreateWordDocument.cs and include the following namespaces in the CreateWordDocument.cs file.
    C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO; 
  1. Use the following code example to create a Word document in the CreateWordDocument.cs file.
    C#
 public string WordDocument(string outputPath)
 {
     try
     {
         // Create a new Word document.
         using (WordDocument document = new WordDocument())
         {
             // Add a section and a paragraph with sample text.
             IWSection section = document.AddSection();
             IWParagraph paragraph = section.AddParagraph();
             paragraph.AppendText("In 2000, AdventureWorks Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the AdventureWorks Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.");
             // Save the document to the specified path.
             using (FileStream outputPathStream = new FileStream(outputPath, FileMode.Create, FileAccess.Write))
             {
                 document.Save(outputPathStream, FormatType.Docx);
             }
         }
         return "Word document created successfully at: " + outputPath;
     }
     catch (Exception ex)
     {
         return "Error: " + ex.Message;
     }
 }

Steps to set up Python to use the C# Library:

  1. Install Python and run the following command in Command Prompt
pip install pythonnet 
  1. Create a Python script named CreateWordDocument.py and set the paths for the DLLs and output file.
import clr #import clr from pythonnet
import os

#Get the DLL and input file path in string
dll_path = r'D:\Create-document\Create-document\bin\Release\netstandard2.0\Create-document.dll' #Dll of application
docio_dll = r'C:\Users\Admin\.nuget\packages\syncfusion.docio.net.core\version\lib\netstandard2.0\Syncfusion.DocIO.Portable.dll'
officeChart_dll = r'C:\Users\Admin\.nuget\packages\syncfusion.officechart.net.core\version\lib\netstandard2.0\Syncfusion.OfficeChart.Portable.dll'
compression_dll =r'C:\Users\Admin\.nuget\packages\syncfusion.compression.net.core\version\lib\netstandard2.0\Syncfusion.Compression.Portable.dll'
license_dll = r'C:\Users\Admin\.nuget\packages\syncfusion.licensing\version\lib\netstandard2.0\Syncfusion.Licensing.dll'

# Verify and add paths
for path in [dll_path, docio_dll, officeChart_dll, compression_dll, license_dll]:
   if path not in os.sys.path:
       os.sys.path.append(path)
       
#load our dll file
try: 
   clr.AddReference(dll_path)
   clr.AddReference(docio_dll)
   clr.AddReference(officeChart_dll)
   clr.AddReference(compression_dll)
   clr.AddReference(license_dll)
   print ("Load success")
except Exception as e:
   print("Fail to load")

#import our DocIO class from Our C# namespace DocIOLibrary
from Create_document import CreateWordDocument

document = CreateWordDocument() #create our DocIO object

# Define file path
output_doc = r"Create-Word-document.docx" #path of result document

# Call CompareDocuments method
result = document.WordDocument(output_doc)

print("Document generated:", result) 

Running the Program:

  1. Run the Python script using the below command.
python CreateWordDocument.py 
  1. The output will be generated successfully in the specified location.

You can download a complete working sample to create a Word document using DocIO in a Python wrapper-based web API from GitHub.

Output Word document

Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Conclusion

I hope you enjoyed learning about how to create a Word document using DocIO in a Python wrapper-based web API.

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied