Python (359)
When importing data from external sources or pasting large volumes of information into Excel, it's common for the data to be placed in a single column. This can make the data difficult to work with, especially when you need to separate it for in-depth analysis. By converting the text into multiple columns, you can create a clearer structure that allows for easier sorting, filtering, and analysis. In this article, we will introduce how to convert text to multiple columns in Excel in Python using Spire.XLS for Python.
Install Spire.XLS for Python
This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.XLS
If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows
Convert Text to Multiple Columns in Excel in Python
Spire.XLS for Python does not offer a direct method for converting text in a cell into multiple columns. However, you can accomplish this by first retrieving the cell content using the CellRange.Text property. Next, use the str.split() method to split the text based on a specified delimiter, such as a comma, space, or semicolon. Finally, write the split data into individual columns. The detailed steps are as follows:
- Create an object of the Workbook class.
- Load an Excel workbook using the Workbook.LoadFromFile() method.
- Access a specific worksheet using the Workbook.Worksheets[index] property.
- Loop through each row in the sheet.
- Get the content of the first cell in the current row using the CellRange.Text property. Next, split the content based on a specified delimiter using the str.split() method, and finally, write the split data into separate columns.
- Automatically adjust column widths in the worksheet using the Worksheet.AllocatedRange.AutoFitColumns() method.
- Save the modified workbook to a new file using the Workbook.SaveToFile() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Specify the input and output Excel File paths
inputFile = "Template.xlsx"
outputFile = "ConvertTextToColumns.xlsx"
# Create an object of the Workbook class
workbook = Workbook()
# Load the Excel file
workbook.LoadFromFile(inputFile)
# Get the first worksheet in the file
sheet = workbook.Worksheets[0]
# Loop through each row in the worksheet
for i in range(sheet.LastRow):
# Get the text of the first cell in the current row
text = sheet.Range[i + 1, 1].Text
# Split the text by comma
splitText = text.split(',')
# Write the split data into individual columns
for j in range(len(splitText)):
sheet.Range[i + 1, j + 2].Text = splitText[j]
# Automatically adjust column widths in the worksheet
sheet.AllocatedRange.AutoFitColumns()
# Save the modified Excel file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.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.
PDF files have different versions, each with unique features and compatibility standards. Changing the version of a PDF can be important when specific versions are required for compatibility with certain devices, software, or regulatory requirements. For instance, you may need to use an older PDF version when archiving or sharing files with users using older software. This article will introduce how to change the version of a PDF document in Python using Spire.PDF for Python.
Install Spire.PDF for Python
This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.PDF
If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows
Change PDF Version in Python
Spire.PDF for Python supports PDF versions ranging from 1.0 to 1.7. To convert a PDF file to a different version, simply set the desired version using the PdfDocument.FileInfo.Version property. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a sample PDF document using the PdfDocument.LoadFromFile() method.
- Change the version of the PDF document to a newer or older version using the PdfDocument.FileInfo.Version property.
- Save the resulting document using the PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Create an object of the PdfDocument class
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("Example.pdf")
# Change the version of the PDF to version 1.7
pdf.FileInfo.Version = PdfVersion.Version1_7
# Save the resulting document
pdf.SaveToFile("ChangePDFVersion.pdf")
pdf.Close()

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.
Python: Remove Backgrounds from PowerPoint Slide or Slide Masters
2024-11-06 01:03:12 Written by KoohjiA well-chosen background can enhance a presentation's appeal, but overly elaborate colors or images may distract viewers and obscure the main message. Additionally, when reusing templates, the original background may not suit the new content. In these cases, removing the background becomes essential to keep your slides clear and focused. This article will show you how to remove backgrounds from PowerPoint slides or slide masters in Python with Spire.Presentation for Python, giving you the flexibility to create clean, professional presentations that keep the audience's attention on what matters.
- Remove Backgrounds from the Specified Slide
- Remove Backgrounds from All Slides
- Remove Backgrounds from PowerPoint Slide Masters
Install Spire.Presentation for Python
This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Presentation
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows
Remove Backgrounds from the Specified Slide
There are typically two types of backgrounds in PowerPoint: background colors and background images. Although these backgrounds differ in their setup, the method to clear them is the same - using the BackgroundType property provided by Spire.Presentation for Python. Let's take a closer look at how to remove backgrounds from a PowerPoint slide with it.
Steps to remove background from a specified slide:
- Create an object for the Presentation class.
- Load a PowerPoint presentation from the local storage using Presentation.LoadFromFile() method.
- Get a certain slide with Presentation.Slides[] method.
- Remove the background by configuring BackgroundType property to none.
- Save the modified PowerPoint presentation using Presentation.SaveToFile() method, and release the memory.
Here is the code example of removing the background on the fourth slide:
- Python
from spire.presentation import *
# Create a Presentation document object
presentation = Presentation()
# Read the presentation document from file
presentation.LoadFromFile("imagebackground.pptx")
# Get the fourth slide
slide = presentation.Slides[3]
# Remove the background by setting the background type
slide.SlideBackground.Type = BackgroundType.none
# Save the modified presentation
presentation.SaveToFile("RemoveBackground.pptx", FileFormat.Pptx2010)
# Release resource
presentation.Dispose()

Remove Backgrounds from All Slides
Batch-deleting all slide backgrounds follows nearly the same steps as deleting a single slide background. The main difference is that you'll need to loop through each slide before setting the background type to ensure no slides are missed.
Steps to remove backgrounds from PowerPoint slides in a batch:
- Instantiate a Presentation class.
- Specify the file path to read a PowerPoint presentation using Presentation.LoadFromFile() method.
- Loop through each slide in the presentation.
- Remove all backgournds by applying BackgroundType.none property to each slide.
- Save the updated PowerPoint presentation as a new file with Presentation.SaveToFile() method, and release the resource.
Below is the code example for removing each background from PowerPoint slides:
- Python
from spire.presentation import *
# Create a Presentation document object
presentation = Presentation()
# Read the presentation document from file
presentation.LoadFromFile("presentation.pptx")
# Loop through each slide
for slide in presentation.Slides:
# Remove the background image or color by setting the background type
slide.SlideBackground.Type = BackgroundType.none
# Save the modified presentation
presentation.SaveToFile("RemoveBackground_allSlides.pptx", FileFormat.Pptx2010)
# Release resource
presentation.Dispose()

How to Remove Backgrounds from PowerPoint Slide Masters
If the slide background still exists after using the above method, you may need to remove the slide master's background instead. Unlike individual slides, setting the background of a slide master applies changes across all slides, so removing the slide master background can efficiently clear all backgrounds at once.
Steps to remove backgrounds from PowerPoint slide masters:
- Create an instance of the Presentation class.
- Load a presentation from the disk with Presentation.LoadFromFile() method.
- Retrieve a specified slide master using Presentation.Masters[] method.
- Access the background of the slide master with Masters.SlideBackground property.
- Remove the background by setting BackgroundType property to none.
- Save the newly modified PowerPoint presentation with Presentation.SaveToFile() method.
Note: Since the process of batch-removing slide master backgrounds is almost similar to deleting background from a slide master, this section will show the steps in the code comments rather than listing them separately.
Here is an example of removing the background from the third slide master:
- Python
from spire.presentation import *
# Create a Presentation object
presentation = Presentation()
# Load the sample file from the disk
presentation.LoadFromFile("presentation.pptx")
# Get the third slide master
master = presentation.Masters[2]
# Access the background of the slide master
SlideBackground = master.SlideBackground
# Clear the background by setting the slide master background style to none
master.SlideBackground.Type = BackgroundType.none
# Loop through each slide master
#for master in presentation.Masters:
# Set the background type to none to remove it
#master.SlideBackground.Type = BackgroundType.none
# Save the result presentation
presentation.SaveToFile("remove_background.pptx", FileFormat.Pptx2013)
# Release resources
presentation.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.
PDF forms are essential tools for collecting information across various industries. Understanding how to import and export this data in different formats like FDF, XFDF, and XML can greatly enhance your data management processes. For instance, importing form data allows you to update or pre-fill PDF forms with existing information, saving time and increasing accuracy. Conversely, exporting form data enables you to share collected information effortlessly with other applications, facilitating seamless integration and minimizing manual entry errors. In this article, we will introduce how to import and export PDF form data in Python using Spire.PDF for Python.
- Import PDF Form Data from FDF, XFDF or XML Files in Python
- Export PDF Form Data to FDF, XFDF or XML Files in Python
Install Spire.PDF for Python
This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.PDF
If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows
Import PDF Form Data from FDF, XFDF or XML Files in Python
Spire.PDF for Python offers the PdfFormWidget.ImportData() method for importing PDF form data from FDF, XFDF, or XML files. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the form of the PDF document using PdfDocument.Form property.
- Import form data from an FDF, XFDF or XML file using PdfFormWidget.ImportData() method.
- Save the resulting document using PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Create an object of the PdfDocument class
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("Forms.pdf")
# Get the form of the document
pdfForm = pdf.Form
formWidget = PdfFormWidget(pdfForm)
# Import PDF form data from an XML file
formWidget.ImportData("Data.xml", DataFormat.Xml)
# Import PDF form data from an FDF file
# formWidget.ImportData("Data.fdf", DataFormat.Fdf)
# Import PDF form data from an XFDF file
# formWidget.ImportData("Data.xfdf", DataFormat.XFdf)
# Save the resulting document
pdf.SaveToFile("Output.pdf")
# Close the PdfDocument object
pdf.Close()

Export PDF Form Data to FDF, XFDF or XML Files in Python
Spire.PDF for Python also enables developers to export PDF form data to FDF, XFDF, or XML files by using the PdfFormWidget.ExportData() method. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the form of the PDF document using PdfDocument.Form property.
- Export form data to an FDF, XFDF or XML file using PdfFormWidget.ExportData() method.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Create an object of the PdfDocument class
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("Forms.pdf")
# Get the form of the document
pdfForm = pdf.Form
formWidget = PdfFormWidget(pdfForm)
# Export PDF form data to an XML file
formWidget.ExportData("Data.xml", DataFormat.Xml, "Form")
# Export PDF form data to an FDF file
# formWidget.ExportData("Data.fdf", DataFormat.Fdf, "Form")
# Export PDF form data to an XFDF file
# formWidget.ExportData("Data.xfdf", DataFormat.XFdf, "Form")
# Close the PdfDocument object
pdf.Close()

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.
The Excel workbook, as a widely used data management tool, can be combined with Python to enable the automation of large-scale data processing. Using Python to set, update, and read cell values in Excel can significantly improve work efficiency, reduce repetitive tasks, and enhance the flexibility and scalability of data processing workflows, thus creating added value. This approach is applicable across a range of fields, from automating financial reports to generating data analysis reports, and can greatly boost productivity in various work contexts.
This article will demonstrate how to set, update, and retrieve cell values in Excel files using Spire.XLS for Python.
- Set cell values in Excel Files with Python
- Update cell values in Excel Files with Python
- Retrieve cell values in Excel Files with Python
Install Spire.XLS for Python
This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.XLS
If you are unsure how to install, please refer to: How to Install Spire.XLS for Python on Windows
Set cell values in Excel Files with Python
We can use the Worksheet.Range.get_Item() method from Spire.XLS for Python to obtain a specified cell in an Excel worksheet as a CellRange object, such as Range.get_Item(2, 1) or Range.get_Item("A2") (row 2, column 1). Then, we can use the CellRange.Value property to set the cell value, or other properties within this class to set text, numbers, boolean values, and other types of data. The following is an example of the procedure:
- Create a Workbook object.
- Get the first default worksheet using Workbook.Worksheets.get_Item() method.
- Obtain the specified cell as a CellRange object using Worksheet.Range.get_Item() method.
- Use properties within the CellRange class, such as Text, Value, DateTimeValue, Formula, and NumberValue, to set cell values.
- Format the cells.
- Save the workbook using Workbook.SaveToFile().
- Python
from spire.xls import Workbook, FileFormat, DateTime, HorizontalAlignType
import datetime
# Create an instance of Workbook to create an Excel workbook
workbook = Workbook()
# Get the first default worksheet
sheet = workbook.Worksheets.get_Item(0)
# Get cell and set text
cell = sheet.Range.get_Item(2, 2)
cell.Text = "Text example"
# Get cell and set a regular value
cell1 = sheet.Range.get_Item(3, 2)
cell1.Value = "$123456"
# Get cell and set a date value
cell2 = sheet.Range.get_Item(4, 2)
cell2.DateTimeValue = DateTime.get_Now()
# Get cell and set a boolean value
cell3 = sheet.Range.get_Item(5, 2)
cell3.BooleanValue = True
# Get cell and set a formula
cell4 = sheet.Range.get_Item(6, 2)
cell4.Formula = "=SUM(B7)"
# Get cell, set a number value, and set number format
cell5 = sheet.Range.get_Item(7, 2)
cell5.NumberValue = 123456
cell5.NumberFormat = "#,##0.00"
# Get cell and set a formula array
cell6 = sheet.Range.get_Item(8, 2)
cell6.HtmlString = "<p><span style='color: blue; font-size: 18px;'>Blue font 18 pixel size</span></p>"
# Set formatting
cellRange = sheet.Range.get_Item(2, 2, 7, 2)
cellRange.Style.Font.FontName = "Arial"
cellRange.Style.Font.Size = 14
cellRange.Style.HorizontalAlignment = HorizontalAlignType.Left
# Auto-fit the column width
sheet.AutoFitColumn(2)
# Save the file
workbook.SaveToFile("output/SetExcelCellValue.xlsx", FileFormat.Version2016)
workbook.Dispose()

Update cell values in Excel Files with Python
To update a cell value in Excel, we can retrieve the cell to update and use the same approach as above to reset its value, thus updating the cell value. Below is an example of the procedure:
- Create a Workbook object.
- Load the Excel file using Workbook.LoadFromFile() method.
- Get a worksheet using Workbook.Worksheets.get_Item() method.
- Obtain the cell to update using Worksheet.Range.get_Item() method.
- Use properties under the CellRange class to reset the cell value.
- Save the workbook with Workbook.SaveToFile() method.
- Python
from spire.xls import Workbook
# Create an instance of Workbook
workbook = Workbook()
# Load the Excel file
workbook.LoadFromFile("output/SetExcelCellValue.xlsx")
# Get the worksheet
sheet = workbook.Worksheets.get_Item(0)
# Get the cell
cell = sheet.Range.get_Item(2, 2)
# Change the cell value to a number
cell.NumberValue = 45150
# Set the cell number format
cell.NumberFormat = "[Green]#,##0;[RED]-#,##0"
# Save the workbook
workbook.SaveToFile("output/UpdateExcelCellValue.xlsx")
workbook.Dispose()

Retrieve cell values in Excel Files with Python
The CellRange.Value property can also be used to directly read cell values. Below is an example of the procedure to read cell values in Excel files:
- Create a Workbook object.
- Load the Excel file with Workbook.LoadFromFile() method.
- Get a worksheet using Workbook.Worksheets.get_Item() method.
- Loop through the specified cell range and use the CellRange.Value property to get the cell value.
- Print the results.
- Python
from spire.xls import Workbook
# Create an instance of Workbook
workbook = Workbook()
# Load the Excel file
workbook.LoadFromFile("output/SetExcelCellValue.xlsx")
# Get the worksheet
sheet = workbook.Worksheets.get_Item(0)
# Loop through cells from row 2 to 8 in column 2
for i in range(2, 8):
# Get the cell
cell = sheet.Range.get_Item(i, 2)
# Get the cell value
value = cell.Value
# Output the value
print(value)
workbook.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.
PDFs often use a variety of fonts and there are situations where you may need to get or replace these fonts. For instance, getting fonts allows you to inspect details such as font name, size, type, and style, which is especially useful for maintaining design consistency or adhering to specific standards. On the other hand, replacing fonts can help address compatibility issues, particularly when the original fonts are not supported on certain devices or software. In this article, we will explain how to get and replace the used fonts in PDF in Python using Spire.PDF for Python.
Install Spire.PDF for Python
This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.PDF
If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows
Get Used Fonts in PDF in Python
Spire.PDF for Python provides the PdfDocument.UsedFonts property to retrieve a list of all fonts used in a PDF. By iterating through this list, you can easily access detailed font information such as the font name, size, type and style using the PdfUsedFont.Name, PdfUsedFont.Size, PdfUsedFont.Type and PdfUsedFont.Style properties. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile() method.
- Get the list of fonts used in this document using the PdfDocument.UsedFonts property.
- Create a text file to save the extracted font information.
- Iterate through the font list.
- Get the information of each font, such as font name, size, type and style using the PdfUsedFont.Name, PdfUsedFont.Size, PdfUsedFont.Type and PdfUsedFont.Style properties, and save it to the text file.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Create an object of the PdfDocument class
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("Input1.pdf")
# Get the list of fonts used in this document
usedFonts = pdf.UsedFonts
# Create a text file to save the extracted font information
with open("font_info.txt", "w") as file:
# Iterate through the font list
for font in usedFonts:
# Get the information of each font, such as font name, size, type and style
font_info = f"Name: {font.Name}, Size: {font.Size}, Type: {font.Type}, Style: {font.Style}\n"
file.write(font_info)
pdf.Close()

Replace Used Fonts in PDF in Python
You can replace the fonts used in a PDF with the desired font using the PdfUsedFont.Replace() method. The detailed steps are as follows.
- Create an object of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile() method.
- Get the list of fonts used in this document using the PdfDocument.UsedFonts property.
- Create a new font using the PdfTrueTypeFont class.
- Iterate through the font list.
- Replace each used font with the new font using the PdfUsedFont.Replace() method.
- Save the resulting document to a new PDF using the PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import *
from spire.pdf import *
# Create an object of the PdfDocument class
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("Input2.pdf")
# Get the list of fonts used in this document
usedFonts = pdf.UsedFonts
# Create a new font
newFont = PdfTrueTypeFont("Arial", 13.0, PdfFontStyle.Italic ,True)
# Iterate through the font list
for font in usedFonts:
# Replace each font with the new font
font.Replace(newFont)
# Save the resulting document to a new PDF
pdf.SaveToFile("ReplaceFonts.pdf")
pdf.Close()

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.
Efficiently emphasizing critical data within Excel workbooks is essential for swift analysis. This process not only draws immediate attention to the most relevant information but also aids in identifying trends, anomalies, and key metrics. By using Python to handle Excel workbooks, users can automate the search and highlight functions, enhancing productivity and ensuring precision. This article explores how to leverage Python for finding and highlighting data in Excel worksheets using Spire.XLS for Python library.
Install Spire.XLS for Python
This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.XLS
If you are unsure how to install, please refer to: How to Install Spire.XLS for Python on Windows
Find and Highlight Data in Excel Worksheets
Using Spire.XLS for Python, we can find all cells containing a specific string and return them as a list by using the Worksheet.FindAllString(stringValue: str, formula: bool, formulaValue: bool) method. After that, we can iterate through the found cells and apply a highlight color by setting it via the CellRange.Style.Color property.
The detailed steps for finding and highlighting data in an Excel worksheet are as follows:
- Create an instance of Workbook class and load an Excel workbook using Workbook.LoadFromFile() method.
- Get a worksheet using Workbook.Worksheets.get_Item() method.
- Find all the cells containing the string to be highlighted using Worksheet.FindAllString() method.
- Iterate through the results to highlight the cells by setting a fill color through CellRange.Style.Color property.
- Save the workbook using Workbook.SaveToFile() method.
- Python
from spire.xls import *
# Create an instance of Workbook
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets.get_Item(0)
# Find the data to be highlighted
cellRanges = sheet.FindAllString("Urgent", False, True)
# Iterate through the found ranges
for cellRange in cellRanges:
# Highlight the data
cellRange.Style.Color = Color.get_LightYellow()
# Save the workbook
workbook.SaveToFile("output/FindHighlightDataExcel.xlsx")
workbook.Dispose()

Find and Highlight Data in a Specific Cell Range
In addition to searching for data across the entire worksheet, we can use the CellRange.FindAllString(stringValue: str, formula: bool, formulaValue: bool) method to find and highlight data within a specified cell range. The detailed steps are as follows:
- Workbook.LoadFromFile() method.
- Get a worksheet using Workbook.Worksheets.get_Item() method.
- Get a cell range through Worksheet.Range[] property.
- Find all the cells containing the string to be highlighted using CellRange.FindAllString() method.
- Iterate through the results to highlight the cells by setting a fill color through CellRange.Style.Color property.
- Save the workbook using Workbook.SaveToFile() method.
- Python
from spire.xls import *
# Create an instance of Workbook
workbook = Workbook()
# Load an Excel file
workbook.LoadFromFile("Sample.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets.get_Item(0)
# Get the cell range
findRange = sheet.Range["C1:C11"]
# Find the data to be highlighted
cellRanges = findRange.FindAllString("Urgent", False, True)
# Iterate the found ranges
for cellRange in cellRanges:
# Highlight the data
cellRange.Style.Color = Color.get_LightYellow()
# Save the workbook
workbook.SaveToFile("output/FindHighlightRange.xlsx")
workbook.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.
Macros in Word documents are small programs created using the Visual Basic for Applications (VBA) language. They are designed to automate repetitive tasks or add advanced functionality. While these macros can be powerful tools for improving productivity, they also pose security risks if used maliciously. Therefore, it is essential to detect and remove potentially harmful macros from Word documents, especially when handling files from untrusted sources. In this article, we will explain how to detect and remove VBA macros in Word documents in Python using Spire.Doc for Python.
- Detect Whether a Word Document Contains VBA Macros in Python
- Remove VBA Macros from a Word Document in Python
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 Windows 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 Windows
Detect Whether a Word Document Contains VBA Macros in Python
Spire.Doc for Python provides the Document.IsContainMacro property, enabling developers to check whether a Word document contains VBA macros easily. This property returns a boolean value: True indicates that the document includes one or more VBA macros, while False indicates that no macros are present in the document.
The following steps explain how to detect whether a Word document contains VBA macros using Spire.Doc for Python:
- Initialize an instance of the Document class.
- Load a Word document using the Document.LoadFromFile() method.
- Detect whether the document includes VBA macros using the Document.IsContainMacro property.
- Python
from spire.doc import *
from spire.doc.common import *
# Initialize an instance of the Document class
document = Document()
# Load a Word document
document.LoadFromFile("Test.docm")
# Detect if the document contains VBA macros
if document.IsContainMacro:
print("The document contains VBA macros.")
else:
print("The document does not contain any VBA macros.")
document.Close()

Remove VBA Macros from a Word Document in Python
Developers can remove all macros from a Word document at once by using the Document.ClearMacros() method. The detailed steps are as follows.
- Initialize an instance of the Document class.
- Load a Word document using the Document.LoadFromFile() method.
- Remove all macros from the document using the Document.ClearMacros() method.
- Save the result document using the Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Initialize an instance of the Document class
document = Document()
# Load a Word document
document.LoadFromFile("Test.docm")
# Remove all VBA macros from the document
document.ClearMacros()
# Save the modified document to a docm file
document.SaveToFile("RemoveMacros.docm", FileFormat.Docm2016)
document.Close()

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.
Textboxes in a Word document serve as versatile containers for text, enabling users to enhance layout and design. They allow for the separation of content from the main body, making documents more visually appealing and organized. Extracting or updating textboxes can be essential for improving document efficiency, ensuring information is current, and facilitating data analysis.
In this article, you will learn how to extract or update textboxes in a Word document using Python and Spire.Doc for Python.
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 Windows 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 Windows
Extract Text from a Textbox in Word
Using Spire.Doc for Python, you can access a specific text box in a document by utilizing the Document.TextBoxes[index] property. After retrieving the text box, you can iterate through its child objects to identify whether each one is a paragraph or a table. If the object is a paragraph, you can retrieve its text using the Paragraph.Text property. In cases where the object is a table, you will need to loop through each cell to extract text from every individual cell within that table.
The steps to extract text from a text box in a Word document are as follows:
- Create a Document object.
- load a Word file by using Document.LoadFromFile() method.
- Access a specific text box using Document.TextBoxes[index] property.
- Iterate through the child objects within the text box.
- Determine if a child object is a paragraph. If it is, retrieve the text from the paragraph using Paragraph.Text property.
- Check if a child object is a table. If so, iterate through the cells in the table to extract text from each cell.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word file
document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx")
# Get a specific textbox
textBox = document.TextBoxes.get_Item(0)
with open('ExtractedText.txt','w') as sw:
# Iterate through the child objects in the textbox
for i in range(textBox.ChildObjects.Count):
# Get a specific child object
object = textBox.ChildObjects.get_Item(i)
# Determine if the child object is paragraph
if object.DocumentObjectType == DocumentObjectType.Paragraph:
# Write paragraph text to txt file
sw.write((object if isinstance(object, Paragraph) else None).Text + "\n")
# Determine if the child object is table
if object.DocumentObjectType == DocumentObjectType.Table:
table = object if isinstance(object, Table) else None
for i in range(table.Rows.Count):
row = table.Rows[i]
for j in range(row.Cells.Count):
cell = row.Cells[j]
for k in range(cell.Paragraphs.Count):
paragraph = cell.Paragraphs.get_Item(k)
# Write paragrah text of a specific cell to txt file
sw.write(paragraph.Text + "\n")
# Dispose resources
document.Dispose()

Update Text in a Textbox in Word
To update a textbox in a Word document, start by clearing its existing content with the TextBox.ChildObjects.Clear() method. This action removes all child objects, including any paragraphs or tables currently contained within the textbox. After clearing the content, you can add a new paragraph to the text box. Once the paragraph is created, set its text to the desired value.
The steps to update a textbox in a Word document are as follows:
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Get a specific textbox using Document.TextBoxes[index] property
- Remove existing content of the textbox using TextBox.ChildObjects.Clear() method.
- Add a paragraph to the textbox using TextBox.Body.AddParagraph() method.
- Add text to the paragraph using Paragraph.AppendText() method.
- Save the document to a different Word file.
- Python
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load a Word file
document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx")
# Get a specific textbox
textBox = document.TextBoxes.get_Item(0)
# Remove child objects of the textbox
textBox.ChildObjects.Clear()
# Add a new paragraph to the textbox
paragraph = textBox.Body.AddParagraph()
# Set line spacing
paragraph.Format.LineSpacing = 15.0
# Add text to the paragraph
textRange = paragraph.AppendText("The text in this textbox has been updated.")
# Set font size
textRange.CharacterFormat.FontSize = 15.0
# Save the document to a different Word file
document.SaveToFile("UpdateTextbox.docx", FileFormat.Docx2019);
# Dispose resources
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.
Python: Save Shapes as Image Files in PowerPoint Presentations
2024-10-21 01:03:20 Written by KoohjiExtracting and repurposing elements from PowerPoint presentations is a valuable skill for cross-platform content sharing. By converting shapes from slides into standalone image files, users can seamlessly integrate them into documents, web pages, or design projects without losing their original formatting and visual effects. With Python, this process becomes straightforward. In this article, we'll explore how to use Spire.Presentation for Python to save shapes from presentation slides as image files with simple Python code.
- Save Shapes from Slides as Image Files with Python
- Save Images from Slides with Formatting as Images Files
Install Spire.Presentation for Python
This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Presentation
If you are unsure how to install, please refer to: How to Install Spire.Presentation for Python on Windows
Save Shapes from Slides as Image Files with Python
Spire.Presentation for Python provides the Slide.Shapes.SaveAsImage(shapIndex: int, dpiX: int, dpiY: int) method to save shapes in presentation slides as images with the specified DPI(optional). With this method, developers can save either a specific shape or all shapes in a PowerPoint presentation. The detailed steps are as follows:
- Create an instance of Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a slide using Presentation.Slides.get_Item() method.
- Iterate through the shapes in the slide:
- Save each shape as an image stream using Slide.Shapes.SaveAsImage() method.
- Save the image stream as an image file using Stream.Save() method.
- Python
from spire.presentation import *
# Create an instance of Presentation
presentation = Presentation()
# Load a PowerPoint file
presentation.LoadFromFile("Sample.pptx")
# Get the first slide
slide = presentation.Slides.get_Item(3)
# Save the shape as an image stream
for i in range(slide.Shapes.Count):
imageStream = slide.Shapes.SaveAsImage(i, 256, 256)
# Save the image
imageStream.Save(f"output/Shapes/ShapeToImage{i}.png")
# Release resources
presentation.Dispose()

Save Images from Slides with Formatting as Images Files
By using the methods provided by Spire.Presentation for Python, developers can also save images from slides as image files while preserving the edits and formatting applied to them. This requires first checking if the shape is an object of SlidePicture class, and if so, the shape can be saved as an image file. The detailed steps are as follows:
- Create an instance of Presentation class.
- Load a PowerPoint presentation using Presentation.LoadFromFile() method.
- Get a slide using Presentation.Slides.get_Item() method.
- Iterate through the shapes in the slide:
- Check if each shape is an object of SlidePicture class.
- If it is, save the shape as an image stream using Slide.Shapes.SaveAsImage() method.
- Save the image stream to a file using Stream.Save() method.
- Python
from spire.presentation import *
# Create an instance of Presentation
presentation = Presentation()
# Load a PowerPoint file
presentation.LoadFromFile("Sample.pptx")
# Get a slide
slide = presentation.Slides.get_Item(4)
# Iterate through all shapes in the slide
i = 0
for shape in slide.Shapes:
# Check if the shape is an object of SlidePicture
if isinstance(shape, SlidePicture):
# Save the shape as an image
shape = shape if isinstance(shape, SlidePicture) else None
image = slide.Shapes.SaveAsImage(slide.Shapes.IndexOf(shape), 256, 256)
image.Save(f"output/Images/ImageShape{i}.png")
i += 1
# Release resources
presentation.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.