Knowledgebase (2311)
Children categories
Page size refers to the dimensions of a document's page. It determines the width and height of the printable area and plays a crucial role in the overall layout and design of the document. Different types of documents may require specific page sizes, such as standard letter size (8.5 x 11 inches) for business letters or A4 size (210 x 297 mm) for international correspondence. Adjusting the page size ensures that your document is compatible with the intended output or presentation medium. In this article, we will demonstrate how to adjust the page size of a Word document in Python using Spire.Doc for Python.
- Adjust the Page Size of a Word Document to a Standard Page Size in Python
- Adjust the Page Size of a Word Document to a Custom Page Size 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 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
Adjust the Page Size of a Word Document to a Standard Page Size in Python
With Spire.Doc for Python, you can easily adjust the page sizes of Word documents to a variety of standard page sizes, such as A3, A4, A5, A6, B4, B5, B6, letter, legal, and tabloid. The following steps explain how to change the page size of a Word document to a standard page size using Spire.Doc for Python:
- Create an instance of the Document class.
- Load a Word document using the Document.LoadFromFile() method.
- Iterate through the sections in the document.
- Set the page size of each section to a standard page size, such as A4, by setting the Section.PageSetup.PageSize property to PageSize.A4().
- Save the result document using the Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an instance of the Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Input.docx")
# Iterate through the sections in the document
for i in range(doc.Sections.Count):
section = doc.Sections.get_Item(i)
# Change the page size of each section to A4
section.PageSetup.PageSize = PageSize.A4()
# Save the result document
doc.SaveToFile("StandardSize.docx", FileFormat.Docx2016)
doc.Close()

Adjust the Page Size of a Word Document to a Custom Page Size in Python
If you plan to print your document on paper with dimensions that don't match any standard paper size, you can change the page size of your document to a custom page size that matches the exact dimensions of the paper. The following steps explain how to change the page size of a Word document to a custom page size using Spire.Doc for Python:
- Create an instance of the Document class.
- Load a Word document using the Document.LoadFromFile() method.
- Create an instance of the SizeF class with customized dimensions.
- Iterate through the sections in the document.
- Set the page size of each section to a custom page size by assigning the SizeF instance to the Section.PageSetup.PageSize property.
- Save the result document using the Document.SaveToFile() method.
- Python
from spire.doc import *
from spire.doc.common import *
# Create an instance of the Document class
doc = Document()
# Load a Word document
doc.LoadFromFile("Input.docx")
# Create an instance of the SizeF class with customized dimensions
customSize = SizeF(600.0, 800.0)
# Iterate through the sections in the document
for i in range(doc.Sections.Count):
section = doc.Sections.get_Item(i)
# Change the page size of each section to the specified dimensions
section.PageSetup.PageSize = customSize
# Save the result document
doc.SaveToFile("CustomSize.docx", FileFormat.Docx2016)
doc.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.
Captions are important elements in a Word document that enhance readability and organizational structure. They provide explanations and supplementary information for images, tables, and other content, improving the clarity and comprehensibility of the document. Captions are also used to emphasize key points and essential information, facilitating referencing and indexing of specific content. By using captions effectively, readers can better understand and interpret data and images within the document while quickly locating the desired information. This article will demonstrate how to use Spire.Doc for .NET to add or remove captions in a Word document within a C# project.
- Add Image Captions to a Word document in C#
- Add Table Captions to a Word document in C#
- Remove Captions from a Word document in C#
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc 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.Doc
Add Image Captions to a Word document in C#
To add captions to images in a Word document, you can achieve it by creating a paragraph, adding an image, and calling the method DocPicture.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) to generate the caption with a specified name, numbering format, and caption position. The following are the detailed steps:
- Create an object of the Document class.
- Use the Document.AddSection() method to add a section.
- Add a paragraph using Section.AddParagraph() method.
- Use the Paragraph.AppendPicture(Image image) method to add a DocPicture image object to the paragraph.
- Use the DocPicture.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) method to add a caption with numbering format as CaptionNumberingFormat.Number.
- Set the Document.IsUpdateFields property to true to update all fields.
- Use the Document.SaveToFile() method to save the resulting document.
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace AddPictureCaption
{
internal class Program
{
static void Main(string[] args)
{
// Create a Word document object
Document document = new Document();
// Add a section
Section section = document.AddSection();
// Add a new paragraph and insert an image
Paragraph pictureParagraphCaption = section.AddParagraph();
pictureParagraphCaption.Format.AfterSpacing = 10;
DocPicture pic1 = pictureParagraphCaption.AppendPicture(Image.FromFile("Data\\1.png"));
pic1.Height = 100;
pic1.Width = 100;
// Add a caption to the image
CaptionNumberingFormat format = CaptionNumberingFormat.Number;
pic1.AddCaption("Image", format, CaptionPosition.BelowItem);
// Add another paragraph and insert another image
pictureParagraphCaption = section.AddParagraph();
DocPicture pic2 = pictureParagraphCaption.AppendPicture(Image.FromFile("Data\\2.png"));
pic2.Height = 100;
pic2.Width = 100;
// Add a caption to the second image
pic2.AddCaption("Image", format, CaptionPosition.BelowItem);
// Update all fields in the document
document.IsUpdateFields = true;
// Save to a docx document
string result = "AddImageCaption.docx";
document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016);
// Close and dispose of the document object to release resources
document.Close();
document.Dispose();
}
}
}

Add Table Captions to a Word document in C#
To add captions to a table in a Word document, you can achieve this by creating the table and using the Table.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) method to generate a numbered caption. The steps involved are as follows:
- Create an object of the Document class.
- Use the Document.AddSection() method to add a section.
- Create a Table object and add it to the specified section in the document.
- Use the Table.ResetCells(int rowsNum, int columnsNum) method to set the number of rows and columns in the table.
- Add a caption to the table using the Table.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) method, specifying the caption numbering format as CaptionNumberingFormat.Number.
- Set the Document.IsUpdateFields property to true to update all fields.
- Use the Document.SaveToFile() method to save the resulting document.
- C#
using Spire.Doc;
namespace AddTableCation
{
internal class Program
{
static void Main(string[] args)
{
// Create a Word document object
Document document = new Document();
// Add a section
Section section = document.AddSection();
// Add a table
Table tableCaption = section.AddTable(true);
tableCaption.ResetCells(3, 2);
// Add a caption to the table
tableCaption.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem);
// Add another table and caption
tableCaption = section.AddTable(true);
tableCaption.ResetCells(2, 3);
tableCaption.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem);
// Update all fields in the document
document.IsUpdateFields = true;
// Save to a docx document
string result = "AddTableCaption.docx";
document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016);
// Close and dispose of the document object to release resources
document.Close();
document.Dispose();
}
}
}

Remove Captions from a Word document in C#
Spire.Doc for .NET can also facilitate the removal of captions from an existing Word document. Here are the detailed steps:
- Create an object of the Document class.
- Use the Document.LoadFromFile() method to load a Word document.
- Create a custom method, named DetectCaptionParagraph(Paragraph paragraph), to determine if a paragraph contains a caption.
- Iterate through all the Paragraph objects in the document using a loop and utilize the custom method, DetectCaptionParagraph(Paragraph paragraph), to identify and delete paragraphs that contain captions.
- Use the Document.SaveToFile() method to save the resulting document.
- C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace DeleteCaptions
{
internal class Program
{
static void Main(string[] args)
{
// Create a Word document object
Document document = new Document();
// Load the example.docx file
document.LoadFromFile("Data/Sample.docx");
Section section;
// Iterate through all sections
for (int i = 0; i < document.Sections.Count; i++)
{
section = document.Sections[i];
// Iterate through paragraphs in reverse order
for (int j = section.Body.Paragraphs.Count - 1; j >= 0; j--)
{
// Check if the paragraph is a caption paragraph
if (DetectCaptionParagraph(section.Body.Paragraphs[j]))
{
// If it's a caption paragraph, remove it
section.Body.Paragraphs.RemoveAt(j);
}
}
}
// Save the document after removing captions
string result = "RemoveCaptions.docx";
document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016);
// Close and dispose of the document object to release resources
document.Close();
document.Dispose();
}
// Method to detect if a paragraph is a caption paragraph
static bool DetectCaptionParagraph(Paragraph paragraph)
{
bool tag = false;
Field field;
// Iterate through the child objects in the paragraph
for (int i = 0; i < paragraph.ChildObjects.Count; i++)
{
if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.Field)
{
// Check if the child object is of Field type
field = (Field)paragraph.ChildObjects[i];
if (field.Type == FieldType.FieldSequence)
{
// Check if the Field type is FieldSequence, indicating a caption field type
return true;
}
}
}
return tag;
}
}
}

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.

Converting HTML to PDF in Python is a common need when you want to generate printable reports, preserve web content, or create offline documentation with consistent formatting. In this tutorial, you’ll learn how to convert HTML to PDF in Python— whether you're working with a local HTML file or a HTML string. If you're looking for a simple and reliable way to generate PDF files from HTML in Python, this guide is for you.
Install Spire.Doc to Convert HTML to PDF Easily
To convert HTML to PDF in Python, you’ll need a reliable library that supports HTML parsing and PDF rendering. Spire.Doc for Python is a powerful and easy-to-use HTML to PDF converter library that lets you generate PDF documents from HTML content — without relying on a browser, headless engine, or third-party tools.
Install via pip
You can install the library quickly with pip:
pip install spire.doc
Alternative: Manual Installation
You can also download the Spire.Doc package and perform a custom installation if you need more control over the environment.
Tip: Spire.Doc offers a free version suitable for small projects or evaluation purposes.
Once installed, you're ready to convert HTML to PDF in Python in just a few lines of code.
Convert HTML Files to PDF in Python
Spire.Doc for Python makes it easy to convert HTML files to PDF. The Document.LoadFromFile() method supports loading various file formats, including .html, .doc, and .docx. After loading an HTML file, you can convert it to PDF by calling Document.SaveToFile() method. Follow the steps below to convert an HTML file to PDF in Python using Spire.Doc.
Steps to convert an HTML file to PDF in Python:
- Create a Document object.
- Load an HTML file using Document.LoadFromFile() method.
- Convert it to PDF using Document.SaveToFile() method.
The following code shows how to convert an HTML file directly to PDF in Python:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load an HTML file
document.LoadFromFile("Sample.html", FileFormat.Html, XHTMLValidationType.none)
# Save the HTML file to a pdf file
document.SaveToFile("output/ToPdf.pdf", FileFormat.PDF)
document.Close()

Convert an HTML String to PDF in Python
If you want to convert an HTML string to PDF in Python, Spire.Doc for Python provides a straightforward solution. For simple HTML content like paragraphs, text styles, and basic formatting, you can use the Paragraph.AppendHTML() method to insert the HTML into a Word document. Once added, you can save the document as a PDF using the Document.SaveToFile() method.
Here are the steps to convert an HTML string to a PDF file in Python.
- Create a Document object.
- Add a section using Document.AddSection() method and insert a paragraph using Section.AddParagraph() method.
- Specify the HTML string and add it to the paragraph using Paragraph.AppendHTML() method.
- Save the document as a PDF file using Document.SaveToFile() method.
Here's the complete Python code that shows how to convert an HTML string to a PDF:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Add a section to the document
sec = document.AddSection()
# Add a paragraph to the section
paragraph = sec.AddParagraph()
# Specify the HTML string
htmlString = """
<html>
<head>
<title>HTML to Word Example</title>
<style>
body {
font-family: Arial, sans-serif;
}
h1 {
color: #FF5733;
font-size: 24px;
margin-bottom: 20px;
}
p {
color: #333333;
font-size: 16px;
margin-bottom: 10px;
}
ul {
list-style-type: disc;
margin-left: 20px;
margin-bottom: 15px;
}
li {
font-size: 14px;
margin-bottom: 5px;
}
table {
border-collapse: collapse;
width: 100%;
margin-bottom: 20px;
}
th, td {
border: 1px solid #CCCCCC;
padding: 8px;
text-align: left;
}
th {
background-color: #F2F2F2;
font-weight: bold;
}
td {
color: #0000FF;
}
</style>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>Here's an unordered list:</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<p>And here's a table:</p>
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
<tr>
<td>John Smith</td>
<td>35</td>
<td>Male</td>
</tr>
<tr>
<td>Jenny Garcia</td>
<td>27</td>
<td>Female</td>
</tr>
</table>
</body>
</html>
"""
# Append the HTML string to the paragraph
paragraph.AppendHTML(htmlString)
# Save the document as a pdf file
document.SaveToFile("output/HtmlStringToPdf.pdf", FileFormat.PDF)
document.Close()

Customize the Conversion from HTML to PDF in Python
While converting HTML to PDF in Python is often straightforward, there are times when you need more control over the output. For example, you may want to set a password to protect the PDF document, or embed fonts to ensure consistent formatting across different devices. In this section, you’ll learn how to customize the HTML to PDF conversion using Spire.Doc for Python.
1. Set a Password to Protect the PDF
To prevent unauthorized viewing or editing, you can encrypt the PDF by specifying a user password and an owner password.
# Create a ToPdfParameterList object
toPdf = ToPdfParameterList()
# Set PDF encryption passwords
userPassword = "viewer"
ownerPassword = "E-iceblue"
toPdf.PdfSecurity.Encrypt(userPassword, ownerPassword, PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit)
# Save as PDF with password protection
document.SaveToFile("/HtmlToPdfWithPassword.pdf", toPdf)
2. Embed Fonts to Preserve Formatting
To ensure the PDF displays correctly across all devices, you can embed all fonts used in the document.
# Create a ToPdfParameterList object
ppl = ToPdfParameterList()
ppl.IsEmbeddedAllFonts = True
# Save as PDF with embedded fonts
document.SaveToFile("/HtmlToPdfWithEmbeddedFonts.pdf", ppl)
These options give you finer control when you convert HTML to PDF in Python, especially for professional document sharing or long-term storage scenarios.
The Conclusion
Converting HTML to PDF in Python becomes simple and flexible with Spire.Doc for Python. Whether you're handling static HTML files or dynamic HTML strings, or need to secure and customize your PDFs, this library provides everything you need — all in just a few lines of code. Get a free 30-day license and start converting HTML to high-quality PDF documents in Python today!
FAQs
Q1: Can I convert an HTML file to PDF in Python? Yes. Using Spire.Doc for Python, you can convert a local HTML file to PDF with just a few lines of code.
Q2: How do I convert HTML to PDF in Chrome? While Chrome allows manual "Save as PDF", it’s not suitable for batch or automated workflows. If you're working in Python, Spire.Doc provides a better solution for programmatically converting HTML to PDF.
Q3: How do I convert HTML to PDF without losing formatting? To preserve formatting:
- Use embedded or inline CSS (not external files).
- Use absolute URLs for images and resources.
- Embed fonts using Spire.Doc options like IsEmbeddedAllFonts(True).