When dealing with a large volume of customized documents such as contracts, reports, or personal letters, the variable feature in Word documents becomes crucial. Variables allow you to store and reuse information like dates, names, or product details, making the documents more personalized and dynamic. This article will delve into how to use Spire.Doc for Python to insert, count, retrieve, and delete variables in Word documents, enhancing the efficiency and flexibility of document management.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Window through the following pip command.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Window

Add Variables into Word Documents with Python

The way Word variables work is based on the concept of "fields". When you insert a variable into a Word document, what you're actually doing is inserting a field, which points to a value stored either in the document properties or an external data source. Upon updating the fields, Word recalculates them to display the most current information.

Spire.Doc for Python offers the VariableCollection.Add(name, value) method to insert variables into Word documents. Here are the detailed steps:

  • Create a Document object.
  • Call the Document.AddSection() method to create a new section.
  • Call the Section.AddParagraph() method to create a new paragraph.
  • Call the Paragraph.AppendField(fieldName, fieldType) method to add a variable field (FieldDocVariable) within the paragraph.
  • Set Document.IsUpdateFields to True to update the fields.
  • Save the document by Document.SaveToFile() method.
  • Python
from spire.doc import *

# Create a Document object
document = Document()

# Add a new section to the document
section = document.AddSection()

# Add a new paragraph within the newly created section
paragraph = section.AddParagraph()

# Append a FieldDocVariable type field named "CompanyName" to the paragraph
paragraph.AppendField("CompanyName", FieldType.FieldDocVariable)

# Add the variable to the document's variable collection
document.Variables.Add("CompanyName", "E-ICEBLUE")

# Update fields
document.IsUpdateFields = True

# Save the document to a specified path
document.SaveToFile("AddVariable.docx", FileFormat.Docx2016)

# Dispose the document
document.Dispose()

Python: Add, Count, Retrieve and Remove Word Variables

Count the Number of Variables in a Word Document with Python

Here are the detailed steps to use the Document.Variables.Count property to get the number of variables:

  • Create a Document object.
  • Call the Document.LoadFromFile() method to load the document that contains the variables.
  • Use the Document.Variables.Count property to obtain the number of variables.
  • Print the count in console.
  • Python
from spire.doc import *

# Create a Document object
document = Document()

# Load an existing document
document.LoadFromFile("AddVariable.docx")

# Get the count of variables in the document
count=document.Variables.Count

# Print to console
print(f"The count of variables:{count}")

Python: Add, Count, Retrieve and Remove Word Variables

Retrieve Variables from a Word Document with Python

Spire.Doc for Python provides the GetNameByIndex(int index) and GetValueByIndex(int index) methods to retrieve variable names and values by their indices. Below are the detailed steps:

  • Create a Document object.
  • Call the Document.LoadFromFile() method to load the document that contains the variables.
  • Call the Document.Variables.GetNameByIndex(index) method to obtain the variable name.
  • Call the Document.Variables.GetValueByIndex(index) method to obtain the variable value.
  • Call the Document.Variables.get_Item(name) to obtain variable value through the variable name.
  • Print the count in console.
  • Python
from spire.doc import *

# Create a Document object
document = Document()

# Load an existing document
document.LoadFromFile("AddVariable.docx")

# Obtain variable name based on index 0
name=document.Variables.GetNameByIndex(0)

# Obtain variable value based on index 0
value=document.Variables.GetValueByIndex(0)

# Obtain variable value through the variable name
value1=document.Variables.get_Item("CompanyName")

# Print to console
print("Variable Name:", name)
print("Variable Value:", value)

Python: Add, Count, Retrieve and Remove Word Variables

Delete Variables from a Word Document with Python

The VariableCollection.Remove(name) method can be used to delete a specified variable from the document, with the parameter being the name of the variable.

  • Create a Document object.
  • Call the Document.LoadFromFile() method to load the document that contains the variables.
  • Call the Document.Variables.Remove(name) method to remove the variable.
  • Set Document.IsUpdateFields to True to update the fields.
  • Save the document by Document.SaveToFile() method.
  • Python
from spire.doc import *

# Create a Document object
document = Document()

# Load an existing document
document.LoadFromFile("AddVariable.docx")

# Remove the variable named "CompanyName"
document.Variables.Remove("CompanyName")

# Update fields
document.IsUpdateFields=True

# Save the document
document.SaveToFile("RemoveVariable.docx",FileFormat.Docx2016)

# Dispose the document
document.Dispose()

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Spire.Doc for Python is a robust library that enables you to read and write Microsoft Word documents using Python. With Spire.Doc, you can create, read, edit, and convert both DOC and DOCX file formats without requiring Microsoft Word to be installed on your system.

This article demonstrates how to install Spire.Doc for Python on Mac.

Step 1

Download the most recent version of Python for macOS and install it on your Mac. If you have already completed this step, proceed directly to step 2.

How to Install Spire.Doc for Python on Mac

Step 2

Open VS Code and search for 'Python' in the Extensions panel. Click 'Install' to add support for Python in your VS Code.

How to Install Spire.Doc for Python on Mac

Step 3

Click 'Explorer' > 'NO FOLRDER OPENED' > 'Open Folder'.

How to Install Spire.Doc for Python on Mac

Choose an existing folder as the workspace, or you can create a new folder and then open it.

How to Install Spire.Doc for Python on Mac

Add a .py file to the folder you just opened and name it whatever you want (in this case, HelloWorld.py).

How to Install Spire.Doc for Python on Mac

Step 4

Use the keyboard shortcut Ctrl + ' to open the Terminal. Then, install Spire.Doc for Python by entering the following command line in the terminal.

pip3 install spire.doc

Note that pip3 is a package installer specifically designed for Python 3.x versions, while pip is a package installer for Python 2.x versions. If you are working with Python 2.x, you can use the pip command.

How to Install Spire.Doc for Python on Mac

Step 5

Open a Terminal window on your Mac, and type the following command to obtain the installation path of Python on your system.

python3 -m pip --version

How to Install Spire.Doc for Python on Mac

Find the libSkiaSharp.dylib file under the site-packages/spire/doc/lib folder. The full path in this case is /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/spire/doc/lib/libSkiaSharp.dylib

How to Install Spire.Doc for Python on Mac

Copy the libSkiaSharp.dylib file into the folder where your .py file is located.

How to Install Spire.Doc for Python on Mac

Step 6

Add the following code snippet to the 'HelloWorld.py' file.

  • Python
from spire.doc.common import *
from spire.doc import *

document = Document()
section = document.AddSection()
paragraph = section.AddParagraph()
paragraph.AppendText("Hello World")
document.SaveToFile("HelloWorld.docx", FileFormat.Docx2019)
document.Dispose()

How to Install Spire.Doc for Python on Mac

After executing the Python file, you will find the resulting Word document in the 'EXPLORER' panel.

How to Install Spire.Doc for Python on Mac

Displaying PowerPoint text box content in multiple columns significantly enhances the presentation of information and audience comprehension. It improves readability by shortening line lengths, making dense text more digestible; optimizes visual layout for an aesthetically pleasing and professional look; and utilizes space efficiently to ensure that information is abundant yet uncluttered. In this article, we will introduce how to add or remove columns in a PowerPoint text box using Spire.Presentation for .NET in C# projects.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Presentation

C# Add Columns to A PowerPoint Text Box

Spire.Presentation provides the Shape.TextFrame.ColumnCount property to set the number of columns for content and the Shape.TextFrame.ColumnSpacing property to set the spacing between columns. Below are the detailed steps:

  • Create a Presentation object.
  • Load a PowerPoint document using the Presentation.LoadFromFile() method.
  • Retrieve the first slide using Presentation.Slides[0].
  • Obtain the first text box object as IAutoShape.
  • Use the Shape.TextFrame.ColumnCount property to set the number of columns for the text box content.
  • Use the Shape.TextFrame.ColumnSpacing property to set the spacing between columns.
  • Save the document to a specified path using the Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;

namespace Spire.PresentationDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {     
            // Create a Presentation object
            Presentation presentation = new Presentation();

            // Load the PPTX file
            presentation.LoadFromFile("Sample1.pptx");

            // Get the first slide
            ISlide slide = presentation.Slides[0];

            // Check if the first shape on the slide is of type IAutoShape
            if (slide.Shapes[0] is IAutoShape)
            {
                // Cast the first shape to an IAutoShape object
                IAutoShape shape = (IAutoShape)slide.Shapes[0];

                // Set the number of columns in the shape's text frame to 2
                shape.TextFrame.ColumnCount = 2;

                // Set the column spacing in the shape's text frame to 25 pt
                shape.TextFrame.ColumnSpacing = 25f;
            }

            // Save the modified presentation as a new PPTX file
            presentation.SaveToFile("SetColumns.pptx", Spire.Presentation.FileFormat.Pptx2016);

            // Dispose of the resources used by the Presentation object
            presentation.Dispose();
        }
    }
}

C# Add or Remove Columns in A PowerPoint Text Box

C# Remove Columns from A PowerPoint Text Box

To remove the columns from the Powerpoint text box, simply set the Shape.TextFrame.ColumnCount property to 1. Below are the detailed steps:

  • Create a Presentation object.
  • Load a PowerPoint document using the Presentation.LoadFromFile() method.
  • Retrieve a slide using the Presentation.Slides[index] property.
  • Obtain the text box object as IAutoShape.
  • Set Shape.TextFrame.ColumnCount = 1 to remove the columns.
  • Save the document to a specified path using the Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;

namespace SpirePresentationDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a Presentation object
            Presentation presentation = new Presentation();

            // Load the PPTX file
            presentation.LoadFromFile("Sample2.pptx");

            // Get the first slide
            ISlide slide = presentation.Slides[0];

            // Check if the first shape on the slide is of type IAutoShape
            if (slide.Shapes[0] is IAutoShape)
            {
                // Cast the first shape to an IAutoShape object
                IAutoShape shape = (IAutoShape)slide.Shapes[0];

                // Set the column count of the shape's text frame to 1
                shape.TextFrame.ColumnCount = 1;
            }

            // Save the modified presentation as a new PPTX file
            presentation.SaveToFile("RemoveColumns.pptx", Spire.Presentation.FileFormat.Pptx2016);

            // Dispose of the resources used by the Presentation object
            presentation.Dispose();
        }
    }
}

C# Add or Remove Columns in A PowerPoint Text Box

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Page 1 of 293