Creating, reading, and updating Word documents is a common need for many developers working with the Python programming language. Whether it's generating reports, manipulating existing documents, or automating document creation processes, having the ability to work with Word documents programmatically can greatly enhance productivity and efficiency. In this article, you will learn how to create, read, or update Word documents in Python using 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

Create a Word Document from Scratch in Python

Spire.Doc for Python offers the Document class to represent a Word document model. A document must contain at least one section (represented by the Section class) and each section is a container for various elements such as paragraphs, tables, charts, and images. This example shows you how to create a simple Word document containing several paragraphs using Spire.Doc for Python.

  • Create a Document object.
  • Add a section using Document.AddSection() method.
  • Set the page margins through Section.PageSetUp.Margins property.
  • Add several paragraphs to the section using Section.AddParagraph() method.
  • Add text to the paragraphs using Paragraph.AppendText() method.
  • Create a ParagraphStyle object, and apply it to a specific paragraph using Paragraph.ApplyStyle() method.
  • Save the document to a Word file using Document.SaveToFile() method.
  • Python
from spire.doc import *	
from spire.doc.common import *

# Create a Document object
doc = Document()

# Add a section
section = doc.AddSection()

# Set the page margins
section.PageSetup.Margins.All = 40

# Add a title
titleParagraph = section.AddParagraph()
titleParagraph.AppendText("Introduction of Spire.Doc for Python")

# Add two paragraphs
bodyParagraph_1 = section.AddParagraph()
bodyParagraph_1.AppendText("Spire.Doc for Python is a professional Python library designed for developers to " +
                           "create, read, write, convert, compare and print Word documents in any Python application " +
                           "with fast and high-quality performance.")

bodyParagraph_2 = section.AddParagraph()
bodyParagraph_2.AppendText("As an independent Word Python API, Spire.Doc for Python doesn't need Microsoft Word to " +
                           "be installed on neither the development nor target systems. However, it can incorporate Microsoft Word " +
                           "document creation capabilities into any developers' Python applications.")

# Apply heading1 to the title
titleParagraph.ApplyStyle(BuiltinStyle.Heading1)

# Create a style for the paragraphs
style2 = ParagraphStyle(doc)
style2.Name = "paraStyle"
style2.CharacterFormat.FontName = "Arial"
style2.CharacterFormat.FontSize = 13
doc.Styles.Add(style2)
bodyParagraph_1.ApplyStyle("paraStyle")
bodyParagraph_2.ApplyStyle("paraStyle")

# Set the horizontal alignment of the paragraphs
titleParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center
bodyParagraph_1.Format.HorizontalAlignment = HorizontalAlignment.Left
bodyParagraph_2.Format.HorizontalAlignment = HorizontalAlignment.Left

# Set the after spacing
titleParagraph.Format.AfterSpacing = 10
bodyParagraph_1.Format.AfterSpacing = 10

# Save to file
doc.SaveToFile("output/WordDocument.docx", FileFormat.Docx2019)

Python: Create, Read, or Update a Word Document

Read Text of a Word Document in Python

To get the text of an entire Word document, you could simply use Document.GetText() method. The following are the detailed steps.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get text from the entire document using Document.GetText() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
doc = Document()

# Load a Word file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\WordDocument.docx")

# Get text from the entire document
text = doc.GetText()

# Print text
print(text)

Python: Create, Read, or Update a Word Document

Update a Word Document in Python

To access a specific paragraph, you can use the Section.Paragraphs[index] property. If you want to modify the text of the paragraph, you can reassign text to the paragraph through the Paragraph.Text property. The following are the detailed steps.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section through Document.Sections[index] property.
  • Get a specific paragraph through Section.Paragraphs[index] property.
  • Change the text of the paragraph through Paragraph.Text property.
  • Save the document to another Word file using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
doc = Document()

# Load a Word file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\WordDocument.docx")

# Get a specific section
section = doc.Sections.get_Item(0)

# Get a specific paragraph
paragraph = section.Paragraphs.get_Item(1)

# Change the text of the paragraph
paragraph.Text = "The title has been changed"

# Save to file
doc.SaveToFile("output/Updated.docx", FileFormat.Docx2019)

Python: Create, Read, or Update a Word Document

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: Convert CSV to PDF

2023-09-04 01:28:28 Written by Koohji

A CSV (Comma-Separated Values) file is a plain text file used to store tabular data. Although CSV files are widely supported by spreadsheet programs, there may still be times when you need to convert them to PDF files to ensure broader accessibility and also enable security features. This article will demonstrate how to convert CSV to PDF 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 CSV to PDF in Python

The Workbook.SaveToFile() method provided by Spire.XLS for Python allows to save a CSV file as a PDF file. The following are the detailed steps.

  • Create a Workbook object.
  • Load a CSV file using Workbook.LoadFromFile() method.
  • Set the Workbook.ConverterSetting.SheetFitToPage property as true to ensure the worksheet is rendered to one PDF page.
  • Get the first worksheet in the Workbook using Workbook.Worksheets[] property.
  • Loop through the columns in the worksheet and auto-fit the width of each column using Worksheet.AutoFitColumn() method.
  • Convert the CSV file to PDF using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load a CSV file
workbook.LoadFromFile("sample.csv", ",", 1, 1)

# Set the SheetFitToPage property as true
workbook.ConverterSetting.SheetFitToPage = True

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Autofit columns in the worksheet
i = 1
while i < sheet.Columns.Length:
    sheet.AutoFitColumn(i)
    i += 1

# Save the CSV file to PDF
workbook.SaveToFile("CSVToPDF.pdf", FileFormat.PDF)
workbook.Dispose()

Python: Convert CSV to PDF

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: Insert Watermarks in Word

2023-09-04 01:18:06 Written by Koohji

A watermark is a semitransparent text or an image placed behind the content of a document. In Word, you can add a watermark to protect the intellectual property of the document, for example to include a copyright symbol, author's name or company logo. Or you can use it to indicate the status of a document, such as "Draft", "Confidential", or "Final". This article will demonstrate how to add text watermarks and image watermarks to Word in Python using 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 commands.

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

Add a Text Watermark to a Word Document in Python

Spire.Doc for Python provides the TextWatermark class to set a text watermark, and then you can add it to Word document through Document.Watermark property. The following are the detailed steps.

  • Create a Document object.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Create an instance of TextWatermark class.
  • Set the text, font size, color and layout of the text watermark using the methods of TextWatermark class.
  • Add the text watermark to the Word document using Document.Watermark property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()

# Load a Word document
document.LoadFromFile("test.docx")

# Create a TextWatermark object
txtWatermark = TextWatermark()

# Set the format of the text watermark
txtWatermark.Text = "DO NOT COPY"
txtWatermark.FontSize = 65
txtWatermark.Color = Color.get_Red()
txtWatermark.Layout = WatermarkLayout.Diagonal

# Add the text watermark to document
document.Watermark = txtWatermark

#Save the result document
document.SaveToFile("Output/TextWatermark.docx", FileFormat.Docx)
document.Close()

Python: Insert Watermarks in Word

Add an Image Watermark in a Word Document in Python

To set the image watermark, you can use the methods of PictureWatermark class. The following are the detailed steps.

  • Create a Document object.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Create an instance of PictureWatermark class.
  • Load an image as the image watermark using PictureWatermark.SetPicture() method, and then set scaling as well as washout property of the image watermark.
  • Add the image watermark to the Word document using Document.Watermark property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()

# Load a Word document
document.LoadFromFile("test.docx")

# Create a PictureWatermark object
picture = PictureWatermark()

# Set the format of the picture watermark
picture.SetPicture("logo.png")
picture.Scaling = 100
picture.IsWashout = False

# Add the image watermark to document
document.Watermark = picture

#Save the result document
document.SaveToFile("Output/ImageWatermark.docx", FileFormat.Docx)
document.Close()

Python: Insert Watermarks in Word

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.

Adding background colors or pictures to your Word documents is a powerful way to enhance their visual appeal and captivate your audience. Whether you're creating a professional report, a creative flyer, or a personal invitation, incorporating a well-chosen background color or image can transform an ordinary document into a visually captivating piece. In this article, we will demonstrate how to add a background color or picture to a Word document in Python using 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

Add a Background Color to Word in Python

You can set a background color for a Word document by changing its background type to "Color" and then selecting a color as the background. The detailed steps are as follows.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the background of the document using Document.Background property.
  • Set the background type as Color using Background.Type property.
  • Set a color as the background using Background.Color property.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Get the document's background
background = document.Background

# Set the background type as Color
background.Type = BackgroundType.Color
# Set the background color
background.Color = Color.get_AliceBlue()

#save the resulting document
document.SaveToFile("AddBackgroundColor.docx", FileFormat.Docx2016)
document.Close()

Python: Add Background Color or Picture to Word Documents

Add a Gradient Background to Word in Python

A gradient background refers to a background style that transitions smoothly between two or more colors. To add a gradient background, you need to change the background type as "Gradient", specify the gradient colors and then set the gradient shading variant and style. The detailed steps are as follows.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the background of the document using Document.Background property.
  • Set the background type as Gradient using Background.Type property.
  • Set two gradient colors using Background.Gradient.Color1 and Background.Gradient.Color2 properties.
  • Set gradient shading variant and style using Background.Gradient.ShadingVariant and Background.Gradient.ShadingStyle properties.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Get the document's background
background = document.Background

# Set the background type as Gradient
background.Type = BackgroundType.Gradient

# Set two gradient colors
background.Gradient.Color1 = Color.get_White()
background.Gradient.Color2 = Color.get_LightBlue()

# Set gradient shading variant and style
background.Gradient.ShadingVariant = GradientShadingVariant.ShadingDown
background.Gradient.ShadingStyle = GradientShadingStyle.Horizontal

#Save the resulting document
document.SaveToFile("AddGradientBackground.docx", FileFormat.Docx2016)
document.Close()

Python: Add Background Color or Picture to Word Documents

Add a Background Picture to Word in Python

To add a background picture to a Word document, you need to change the background type as "Picture", and then set a picture as the background. The detailed steps are as follows.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the background of the document using Document.Background property.
  • Set the background type as Picture using Background.Type property.
  • Set a picture as the background using Background.SetPicture() method.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Sample.docx")

# Get the document's background
background = document.Background

# Set the background type as Picture
background.Type = BackgroundType.Picture

# Set the background picture
background.SetPicture("background.jpg")

#save the resulting document
document.SaveToFile("AddBackgroundPicture.docx", FileFormat.Docx2016)
document.Close()

Python: Add Background Color or Picture to Word Documents

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: Merge or Unmerge Cells in Excel

2023-09-01 01:22:56 Written by Koohji

Merging cells means combining multiple adjacent cells into a larger one. The merged cell will inherit all the properties and contents of the original cells. This feature is particularly useful when you need to create a larger cell to accommodate more content or create a header row. Unmerging cells, on the other hand, involves reverting the merged cells back to the original multiple cells. The unmerged cells will revert back to their original independent state, and you can input different content into each individual cell. Merging and unmerging cells are common operations in spreadsheet software, allowing you to adjust the layout and structure of a table as needed, making the data clearer and easier to understand. In this article, you will learn how to merge or unmerge cells in Excel in Python by 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

Merge the Cells of the Specified Row or Column

With Spire.XLS for Python, users are able to effortlessly merge the cells of the specific column or row in Excel, thereby enhancing their data manipulation capabilities. The following are the detailed steps.

  • Create an object of Workbook class.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get the desired worksheet by using Workbook.Worksheets[] property.
  • Access the cells of the specific column or row and merge them by calling Worksheet.Columns[].Merge() or Worksheet.Rows[].Merge() methods.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "Sample.xlsx"
outputFile = "MergeRowColumn.xlsx"

#Create an object of Workbook class
workbook = Workbook()

#Load a sample Excel file from disk
workbook.LoadFromFile(inputFile)

#Get the first worksheet of this file
sheet = workbook.Worksheets[0]

#Merge the first column in Excel 
#sheet.Columns[0].Merge()

#Merge the first row in Excel 
sheet.Rows[0].Merge()

#Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Merge or Unmerge Cells in Excel

Merge Ranges of Cells

In addition to merging the specific column or row, Spire.XLS for Python also supports users to merge the specified cell ranges. The following are the detailed steps.

  • Create an object of Workbook class.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get the desired worksheet by using Workbook.Worksheets[] property.
  • Access the specific range of cells and merge them together by calling Worksheet.Range[].Merge() method.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "Sample.xlsx"
outputFile = "MergeCellRange.xlsx"

#Create an object of Workbook class
workbook = Workbook()

#Load a sample Excel file from disk
workbook.LoadFromFile(inputFile)

#Get the first worksheet of this file
sheet = workbook.Worksheets[0]

#Merge the particular cell range in Excel 
sheet.Range["B6:G6"].Merge()

#Save the result file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Merge or Unmerge Cells in Excel

Unmerge the Cells of the Specified Row or Column

Additionally, users are also allowed to unmerge the merged cells of the specific column or row at any time with Spire.XLS for Python. The following are the detailed steps.

  • Create an object of Workbook class.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get the desired worksheet by using Workbook.Worksheets[] property.
  • Access the merged cells of the specific column or row and unmerge them by calling Worksheet.Columns[].UnMerge() and Worksheet.Rows[].UnMerge() methods.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "MergeRowColumn.xlsx"
outputFile = "UnmergeRowColumn.xlsx"

#Create an object of Workbook class
workbook = Workbook()

#Load a sample file from disk
workbook.LoadFromFile(inputFile)

#Get the first worksheet of this file
sheet = workbook.Worksheets[0]

#Unmerge the first column in Excel
#sheet.Columns[0].UnMerge()

#Unmerge the first column in Excel
sheet.Rows[0].UnMerge()

#Save to file.
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Merge or Unmerge Cells in Excel

Unmerge Ranges of Cells

What's more, users are also able to unmerge the specified cell ranges using Spire.XLS for Python. The following are the detailed steps.

  • Create an object of Workbook class.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get the desired worksheet by using Workbook.Worksheets[] property.
  • Access the specific cell ranges and unmerge them by calling Worksheet.Range[].UnMerge() method.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "MergeCellRange.xlsx"
outputFile = "UnmergeCellRange.xlsx"

#Create an object of Workbook class
workbook = Workbook()

#Load a sample file from disk
workbook.LoadFromFile(inputFile)

#Get the first worksheet of this file
sheet = workbook.Worksheets[0]

#Unmerge the particular cell range in Excel
sheet.Range["B6:G6"].UnMerge()

#Save to file.
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Merge or Unmerge Cells in Excel

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: Add Hyperlinks to Excel

2023-08-31 07:04:46 Written by Koohji

Hyperlinks are a useful tool in Microsoft Excel that allows users to create clickable links within their spreadsheets. By adding hyperlinks, you can conveniently navigate between different sheets, workbooks, websites, or even specific cells within the same workbook. Whether you need to reference external resources, connect related data, or create interactive reports, hyperlinks can help you achieve your purpose with ease. In this article, we will demonstrate how to add hyperlinks to 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

Add Text Hyperlinks to Excel in Python

Text hyperlinks in Excel are clickable words or phrases that can direct users to different parts of the Excel file, external resources, or email addresses. The following steps explain how to add a text hyperlink to an Excel file using Spire.XLS for Python:

  • Create a Workbook object.
  • Get the desired worksheet using Workbook.Worksheets[] property.
  • Access the specific cell that you want to add a hyperlink to using Worksheet.Range[] property.
  • Add a hyperlink to the cell using Worksheet.HyperLinks.Add() method.
  • Set the type, display text and address of the hyperlink using XlsHyperLink.Type, XlsHyperLink.TextToDisplay and XlsHyperLink.Address properties.
  • Save the resulting file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Add a text hyperlink that leads to a webpage
cell1 = sheet.Range["B3"]
urlLink = sheet.HyperLinks.Add(cell1)
urlLink.Type = HyperLinkType.Url
urlLink.TextToDisplay = "Link to a website"
urlLink.Address = "https://www.e-iceblue.com/"

# Add a text hyperlink that leads to an email address
cell2 = sheet.Range["E3"]
mailLink = sheet.HyperLinks.Add(cell2)
mailLink.Type = HyperLinkType.Url
mailLink.TextToDisplay = "Link to an email address"
mailLink.Address = "mailto:example@outlook.com"

# Add a text hyperlink that leads to an external file
cell3 = sheet.Range["B7"]
fileLink = sheet.HyperLinks.Add(cell3)
fileLink.Type = HyperLinkType.File
fileLink.TextToDisplay = "Link to an external file"
fileLink.Address = "C:\\Users\\Administrator\\Desktop\\Report.xlsx"

# Add a text hyperlink that leads to a cell in another sheet
cell4 = sheet.Range["E7"]
linkToSheet = sheet.HyperLinks.Add(cell4)
linkToSheet.Type = HyperLinkType.Workbook
linkToSheet.TextToDisplay = "Link to a cell in sheet2"
linkToSheet.Address = "Sheet2!B5"

# Add a text hyperlink that leads to a UNC address
cell5 = sheet.Range["B11"]
uncLink = sheet.HyperLinks.Add(cell5)
uncLink.Type = HyperLinkType.Unc
uncLink.TextToDisplay = "Link to a UNC address"
uncLink.Address = "\\\\192.168.0.121"

# Autofit column widths
sheet.AutoFitColumn(2)
sheet.AutoFitColumn(5)

# Save the resulting file
workbook.SaveToFile("AddTextHyperlinks.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python: Add Hyperlinks to Excel

Add Image Hyperlinks to Excel in Python

Image hyperlinks in Excel work similarly to text hyperlinks but use images as clickable elements instead of words or phrases. They provide a visually appealing and intuitive way to navigate within the spreadsheet or to external resources. The following steps explain how to add an image hyperlink to an Excel file using Spire.XLS for Python:

  • Create a Workbook object.
  • Get the desired worksheet using Workbook.Worksheets[] property.
  • Insert an image into the worksheet using Worksheet.Pictures.Add() method.
  • Add a hyperlink to the image using XlsBitmapShape.SetHyperLink() method.
  • Save the result file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Add text to the worksheet
sheet.Range["B2"].Text = "Image Hyperlink"
# Set the width of the second column
sheet.Columns[1].ColumnWidth = 15

# Insert an image into the worksheet
picture = sheet.Pictures.Add(3, 2, "logo2.png")

# Add a hyperlink to the image 
picture.SetHyperLink("https://www.e-iceblue.com", True)
            
# Save the resulting file
workbook.SaveToFile("AddImageHyperlink.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

Python: Add Hyperlinks to Excel

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.Presentation for Python is a Python library for reading, creating, editing and converting PowerPoint (.ppt or .pptx) files in any Python application. This article shows you how to install Spire.Presentation for Python on Windows.

Step 1

Download the latest version of Python and install it on your computer. If you have already installed it, skip to step 2.

How to Install Spire.Presentation for Python in VS Code

Step 2

Click "Extensions" in VS Code, search for "Python" and then install it.

How to Install Spire.Presentation for Python in VS Code

Step 3

Click "Explorer" - "NO FOLRDER OPENED" - "Open Folder".

How to Install Spire.Presentation for Python in VS Code

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

How to Install Spire.Presentation for Python in VS Code

Add a .py file to the folder you just added (Python folder in this case), and name it whatever you like.

How to Install Spire.Presentation for Python in VS Code

Step 4

Click "Terminal" and then "New Terminal".

How to Install Spire.Presentation for Python in VS Code

Input the following pip command to install Spire.Presentation for Python and plum-dispatch v1.7.4.

pip install Spire.Presentation

How to Install Spire.Presentation for Python in VS Code

Alternatively, you can download Spire.Presentation for Python from our website,  and unzip it to get two .whl files from the "lib" folder. They're for Linux system and Windows system respectively.

How to Install Spire.Presentation for Python in VS Code

After that, install Spire.Presentation for Python and plum-dispatch v1.7.4 by running the following pip command.

pip install E:\Library\Python\spire.presentation.python_8.8.3\lib\Spire.Presentation-8.8.3-py3-none-win_amd64.whl

How to Install Spire.Presentation for Python in VS Code

Step 5

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

How to Install Spire.Presentation for Python in VS Code

Once you run the Python file, you'll see the result PowerPoint document in the "EXPORER" panel.

How to Install Spire.Presentation for Python in VS Code

Spire.Presentation for Python is a professional presentation processing API that is highly compatible with PowerPoint. It is a completely independent class library that developers can use to create, edit, convert, and save PowerPoint presentations efficiently without installing Microsoft PowerPoint.

Spire.Presentation for Python supports a variety of presentation manipulation features, such as adding/deleting/hiding/showing/rearranging/copying slides, adding/extracting images, adding/removing hyperlinks, adding/extracting animations, creating tables/charts, adding/extracting/highlighting/replacing text, adding/extracting/replacing videos and audio, encrypting/decrypting presentations, adding text/image watermarks, setting/removing background, and manipulating comments/speaker note etc.

Spire.PDF for Python is a Python library for reading, creating, editing and converting PDF files in any Python application. This article shows you how to install Spire.PDF for Python on Windows.

Step 1

Download the latest version of Python and install it on your computer. If you have already installed it, skip to step 2.

How to Install Spire.PDF for Python in VS Code

Step 2

Click "Extensions" in VS Code, search for "Python" and then install it.

How to Install Spire.PDF for Python in VS Code

Step 3

Click "Explorer" - "NO FOLRDER OPENED" - "Open Folder".

How to Install Spire.PDF for Python in VS Code

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

How to Install Spire.PDF for Python in VS Code

Add a .py file to the folder you just added (Python folder in this case), and name it whatever you like.

How to Install Spire.PDF for Python in VS Code

Step 4

Click "Terminal" and then "New Terminal".

How to Install Spire.PDF for Python in VS Code

Input the following pip command to install Spire.PDF for Python and plum-dispatch v1.7.4.

pip install Spire.PDF

How to Install Spire.PDF for Python in VS Code

Alternatively, you can download Spire.PDF for Python from our website,  and unzip it to get two .whl files from the "lib" folder. They're for Linux and Windows systems, respectively.

How to Install Spire.PDF for Python in VS Code

Then, install Spire.PDF for Python and plum-dispatch v1.7.4 by running the following pip command.

pip install E:\Library\Python\spire.pdf.python_9.8.0\lib\spire.pdf-9.8.0-py3-none-win_amd64.whl

How to Install Spire.PDF for Python in VS Code

Step 5

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

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

doc= PdfDocument()
page = doc.Pages.Add()
str = "Hello, World"
x = 10.0
y = 10.0
font = PdfFont(PdfFontFamily.Helvetica ,30.0)
color = PdfRGBColor(Color.get_Black())
textBrush = PdfSolidBrush(color)
page.Canvas.DrawString(str, font, textBrush, x, y)
doc.SaveToFile("output.pdf")
doc.Close()

How to Install Spire.PDF for Python in VS Code

Once you run the Python file, you'll see the result PDF document in the 'EXPORER' panel.

How to Install Spire.PDF for Python in VS Code

Python: Set or Change Fonts in Excel

2023-08-25 07:05:38 Written by Koohji

Fonts play a crucial role in enhancing the visual appeal and readability of data in Microsoft Excel. Whether you're creating a spreadsheet, designing a report, or simply organizing information, the ability to set or change fonts can greatly impact the overall presentation. Excel offers a wide range of font options, allowing you to customize the style, size, and formatting to suit your specific needs. In this article, you will learn how to set or change fonts 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

Set Different Fonts for Different Cells in Python

With Spire.XLS for Python, customizing fonts in specific cells becomes a breeze. By utilizing the CellRange.Style.Font property, you gain control over font name, color, size, and style effortlessly. Follow these steps to apply a font style to a particular cell using Spire.XLS for Python.

  • Create a Workbook object.
  • Get a specific worksheet through Workbook.Worksheets[index] property.
  • Get a specific cell through Worksheet.Range[int Row, int Column] property.
  • Set the value of the cell through CellRange.Value property.
  • Set the font name, color, size and style of the cell value through the properties under the CellRange.Style.Font object.
  • Save the workbook to an Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object 
workbook = Workbook()

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Set font name
row = 1
sheet.Range[row, 1].Value = "Font Name"
sheet.Range[row, 2].Value = "Arial Black"
sheet.Range[row, 2].Style.Font.FontName = "Arial Black"

# Set font size
row += 2
sheet.Range[row, 1].Value = "Font Size"
sheet.Range[row, 2].Value = "15"
sheet.Range[row, 2].Style.Font.Size = 15

# Set font color 
row += 2
sheet.Range[row, 1].Value = "Font Color"
sheet.Range[row, 2].Value = "Red"
sheet.Range[row, 2].Style.Font.Color = Color.get_Red()

# Make text bold
row += 2
sheet.Range[row, 1].Value = "Bold"
sheet.Range[row, 2].Value = "Bold"
sheet.Range[row, 2].Style.Font.IsBold = True;

# Make text italic 
row += 2
sheet.Range[row, 1].Value = "Italic"
sheet.Range[row, 2].Value = "Italic"
sheet.Range[row, 2].Style.Font.IsItalic = True

# Underline text
row += 2
sheet.Range[row, 1].Value = "Underline"
sheet.Range[row, 2].Value = "Underline"
sheet.Range[row, 2].Style.Font.Underline = FontUnderlineType.Single

# Strikethrough text 
row += 2
sheet.Range[row, 1].Value = "Strikethrough "
sheet.Range[row, 2].Value = "Strikethrough "
sheet.Range[row, 2].Style.Font.IsStrikethrough = True

# Set column width
sheet.Columns[0].ColumnWidth = 25
sheet.Columns[1].ColumnWidth = 25

# Save the workbook to an Excel file
workbook.SaveToFile("output/ApplyFontInCell.xlsx", ExcelVersion.Version2016)

Python: Set or Change Fonts in Excel

Apply Multiple Fonts in a Single Cell in Python

To emphasize specific characters within a cell, you can mix fonts. Here are the steps to apply multiple fonts in a single cell using Spire.XLS for Python:

  • Create a Workbook object.
  • Get a specific worksheet through Workbook.Worksheets[index] property.
  • Create two ExcelFont objects using Workbook.CreateFont() method.
  • Get a specific cell through Worksheet.Range[int Row, int Column] property, and set the rich text content of the cell through CellRange.RichText.Text property.
  • Apply the two ExcelFont objects to the rich text using RichText.SetFont() method.
  • Save the workbook to an Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Create a font
font1 = workbook.CreateFont()
font1.FontName = "Arial Black"
font1.KnownColor = ExcelColors.LightBlue
font1.IsBold = True
font1.Size = 13

# Create another font
font2 = workbook.CreateFont()
font2.KnownColor = ExcelColors.Red
font2.IsBold = True
font2.IsItalic = True
font2.FontName = "Algerian"
font2.Size = 15;

# Returns a RichText object from a specified cell
richText = sheet.Range["A1"].RichText

# Set the text of RichText object
richText.Text = "Buy One, Get One Free"

# Apply the first font to specified range of characters
richText.SetFont(0, 16, font1)

# Apply the second font to specified range of characters
richText.SetFont(17, 21, font2)

# Set column width
sheet.Columns[0].ColumnWidth = 33

# Save the workbook to an Excel file
workbook.SaveToFile("output/ApplyMultipleFontsInSingleCell.xlsx", ExcelVersion.Version2016)

Python: Set or Change Fonts in Excel

Change the Font Style of a Cell Range in Python

Spire.XLS for Python offers the CellStyle class, enabling users to handle cell formatting like fill color, text alignment, and font style. By creating a cell style, you can apply it to a specific range of cells using the CellRange.ApplyStyle() method or to an entire worksheet using the Worksheet.ApplyStyle() method. To change the font style of a cell range using Spire.XLS for Python, follow these steps.

  • Create a Workbook object.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[index] property.
  • Create a CellStyle object using Workbook.Styles.Add() method, and set the font style through the CellStyle.Font property.
  • Apply the cell style to a cell range using CellRange.ApplyStyle() method.
  • Save the workbook to another Excel file using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Create a Workbook object
workbook = Workbook()

# Load a sample Excel file
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx")

# Get the first worksheet
sheet = workbook.Worksheets[0]

# Create a CellStyle object
fontStyle = workbook.Styles.Add("headerFontStyle")

# Set the font color, size and style
fontStyle.Font.Color = Color.get_White()
fontStyle.Font.IsBold = True
fontStyle.Font.Size = 12

# Create a CellStyleFlag object, setting the FontColor, FontBold, ad FontSize properties to true
flag = CellStyleFlag()
flag.FontColor = True
flag.FontBold = True
flag.FontSize = True

# Apply the cell style to header row 
sheet.Range[1, 1, 1, 8].ApplyStyle(fontStyle, flag)

# Apply the cell style to the whole worksheet
# sheet.ApplyStyle(fontStyle)

# Save the workbook to another Excel file
workbook.SaveToFile("output/ApplyFontToCellRange.xlsx", ExcelVersion.Version2016)

Python: Set or Change Fonts in Excel

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 25 of 26
page 25