冰蓝科技
|
028-81705109
|
|
微信扫一扫
|

Spire.Cloud 纯前端文档控件

Spire.OCR for Java 1.9.2 已发布。本次更新修复了在JDK17和JDK21下运行时程序抛出异常的问题。详情请阅读以下内容。

问题修复:


获取 Spire.OCR for Java 1.9.2请点击:

https://www.e-iceblue.cn/Downloads/Spire-OCR-JAVA.html

Spire.XLS for Python 13.12.6 已发布。本次更新增加自定义异常类 SpireException 同时将 ArgumentError 类变更为 SpireException 类。详情请阅读以下内容。

新功能:


获取 Spire.XLS for Python 13.12.6请点击:

https://www.e-iceblue.cn/Downloads/Spire-XLS-Python.html

题注在 Word 文档中是提高可理解性和组织结构的重要元素。它提供对图片、表格等内容的解释和补充信息,增强文档的可读性和清晰度。题注还用于强调重点和关键信息,方便引用和索引特定内容。通过合理使用题注,读者能更好地理解和解读文档中的数据和图像,并能快速定位所需信息。本文将介绍如何使 Spire.Doc for .NET 在 C# 项目中添加和删除 Word 文档中的题注

安装 Spire.Doc for .NET

首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。

PM> Install-Package Spire.Doc

添加图片题注到 Word 文档

实现向 Word 文档中的图片添加题注,即创建段落、添加图片内容,以及调用 DocPicture.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) 方法来生成题注的编号。详细步骤如下:

  • 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)
        {
            // 创建Word文档对象
            Document document = new Document();
            // 添加一个章节
            Section section = document.AddSection();
            // 添加一个新段落并给它添加一个图片
            Paragraph pictureParagraphCaption = section.AddParagraph();
            pictureParagraphCaption.Format.AfterSpacing = 10;
            DocPicture pic1 = pictureParagraphCaption.AppendPicture(Image.FromFile("Data\\1.png"));
            pic1.Height = 100;
            pic1.Width = 100;
            // 给图片添加题注
            CaptionNumberingFormat format = CaptionNumberingFormat.Number;
            pic1.AddCaption("图片", format, CaptionPosition.BelowItem);
            // 再新添加一个新段落并给它添加一个图片
            pictureParagraphCaption = section.AddParagraph();
            DocPicture pic2 = pictureParagraphCaption.AppendPicture(Image.FromFile("Data\\2.png"));
            pic2.Height = 100;
            pic2.Width = 100;
            // 给图片添加题注
            pic2.AddCaption("图片", format, CaptionPosition.BelowItem);
            // 更新文档中的所有的域
            document.IsUpdateFields = true;
            // 保存到一个docx文档
            string result = "添加图片题注.docx";
            document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016);
            // 关闭document对象释放资源
            document.Close();
            document.Dispose();
        }
     }
}

C# 添加和删除 Word 中的题注

添加表格题注到 Word 文档

实现向 Word 文档中的表格添加题注,即创建表格,调用 Table.AddCaption(string name, CaptionNumberingFormat format, CaptionPosition captionPosition) 方法来生成题注的编号。详细步骤如下:

  • C#
using Spire.Doc;
namespace AddTableCation
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建Word文档对象
            Document document = new Document();
            // 添加一个章节
            Section section = document.AddSection();
            // 添加一个表格
            Table tableCaption = section.AddTable(true);
            tableCaption.ResetCells(3, 2);
            // 给表格添加题注
            tableCaption.AddCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.BelowItem);
            // 再新添加一个表格并给表格添加题注
            tableCaption = section.AddTable(true);
            tableCaption.ResetCells(2, 3);
            tableCaption.AddCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.BelowItem);
            // 更新文档中的所有的域
            document.IsUpdateFields = true;
            // 保存到一个docx文档
            string result = "添加表格题注.docx";
            document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016);
            // 关闭document对象释放资源
            document.Close();
            document.Dispose();
        }
    }
}

C# 添加和删除 Word 中的题注

从 Word 文档中删除题注

Spire.Doc for .NET 还可以实现从存在题注的 Word 文档中将题注进行删除。详细步骤如下:

  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace DeleteCaptions
{
    internal class Program1
    {
        static void Main(string[] args)
        {
            // 创建Word文档对象
            Document document = new Document();
            // 加载示例.docx文件
            document.LoadFromFile("https://cdn.e-iceblue.cn/Data/示例.docx");
            Section section;
            // 遍历所有节
            for (int i = 0; i < document.Sections.Count; i++)
            {
                section = document.Sections[i];
                // 倒序遍历节中的段落
                for (int j = section.Body.Paragraphs.Count - 1; j >= 0; j--)
                {
                    // 检测段落是否为题注段落
                    if (DetectCaptionParagraph(section.Body.Paragraphs[j]))
                    {
                        // 如果是题注段落,则移除该段落
                        section.Body.Paragraphs.RemoveAt(j);
                    }
                }
            }
            // 保存删除题注后的文档
            string result = "删除题注.docx";
            document.SaveToFile(result, Spire.Doc.FileFormat.Docx2016);
            // 关闭document对象释放资源
            document.Close();
            document.Dispose();
        }
        // 判断段落是否为题注段落的方法
        static bool DetectCaptionParagraph(Paragraph paragraph)
        {
            bool tag = false;
            Field field;
            // 遍历段落中的子对象
            for (int i = 0; i < paragraph.ChildObjects.Count; i++)
            {
                if (paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.Field)
                {
                    // 判断子对象是否为Field类型
                    field = (Field)paragraph.ChildObjects[i];
                    if (field.Type == FieldType.FieldSequence)
                    {
                        // 判断Field类型是否为FieldSequence,即题注域类型
                        return true;
                    }
                }
            }
            return tag;
        }
    }
}

C# 添加和删除 Word 中的题注

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

通过设置表格格式,可以确保整个文档中的表格具有一致的外观和风格。这有助于提高文档的专业性和可读性,使读者能够方便地浏览相关内容,并且促进了文档的易读性和互动性。本文将介绍如何使用 Spire.Doc for Python 在 Python 中设置 Word 文档中的表格格式

安装 Spire.Doc for Python

本教程需要使用 Spire.Doc for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。

pip install Spire.Doc

如果您不确定如何安装,请参考此教程:如何在 Windows 中安装 Spire.Doc for Python

Python 设置 Word 文档中的表格内置样式

Spire.Doc for Python 提供了 Table.ApplyStyle() 方法,可以将不同的样式应用到表格上。下面是详细的步骤:

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

# 设置输出文件路径
outputFile = "output/SetTableStyle.docx"

# 设置输入文件路径
inputFile = "data/Table.docx"

# 创建文档对象
document = Document()

# 从输入文件中加载文档内容
document.LoadFromFile(https://cdn.e-iceblue.cn/inputFile)

# 获取文档的第一个节
section = document.Sections.get_Item(0)

# 获取第一个节中的表格,如果不存在则返回None
table = section.Tables.get_Item(0) if isinstance(section.Tables.get_Item(0), Table) else None

# 应用默认的彩色列表样式到表格上
table.ApplyStyle(DefaultTableStyle.LightGridAccent3)

# 将修改后的文档保存到输出文件中
document.SaveToFile(outputFile, FileFormat.Docx)

# 关闭文档对象
document.Close()

Python 设置 Word 文档中的表格格式

Python 设置 Word 文档中的表格段落文字样式

Spire.Doc for Python 提供了 Document.Styles.Add() 方法添加自定义的段落样式,再通过 Paragraph.ApplyStyle() 方法,可以将自定义的样式应用到表格上。下面是详细的步骤:

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

# 定义输出文件路径
outputFile = "output/SetTableStyle.docx"

# 定义输入文件路径
inputFile = "data/CreateTable.docx"

# 创建文档对象
document = Document()

# 从输入文件中加载文档内容
document.LoadFromFile(inputFile)

# 获取文档的第一个章节
section = document.Sections[0]

# 获取第一个章节中的第一个表格,如果不存在则返回None
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None

# 创建段落样式对象
style =  ParagraphStyle(document)

# 设置段落样式名称为"TableStyle"
style.Name = "TableStyle"    
      
# 设置段落字体大小为14.0
style.CharacterFormat.FontSize = 14.0

# 设置段落文本颜色为绿色
style.CharacterFormat.TextColor = Color.get_SeaGreen()

# 设置段落高亮颜色为黄色
style.CharacterFormat.HighlightColor = Color.get_Yellow()

# 将自定义的段落样式添加到文档样式集合中
document.Styles.Add(style)

# 将自定义的段落样式应用到表格的第一行第一列的第一个段落上
table.Rows[0].Cells[0].Paragraphs[0].ApplyStyle(style.Name)

# 将修改后的文档保存到输出文件中
document.SaveToFile(outputFile, FileFormat.Docx)

# 关闭文档对象
document.Close()

Python 设置 Word 文档中的表格格式

Python 设置 Word 文档中表格的自适应方式

Spire.Doc for Python 提供了 Table.AutoFit() 方法用于自动调整表格以适应内容。在 Word 文档中,可以通过这个方法来确保表格中的文本能够正确显示,并且不会超出单元格的边界。下面是详细的步骤:

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

inputFile = "data/TableSample.docx"
outputFile = "output/AutoFitToContents.docx"

# 创建一个文档对象
document = Document()

# 从输入文件中加载文档内容
document.LoadFromFile(inputFile)

# 获取文档的第一个章节
section = document.Sections[0]

# 获取第一个章节中的第一个表格,如果不存在则返回None
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None

# 自动调整表格的宽度以适应内容
table.AutoFit(AutoFitBehaviorType.AutoFitToContents)

# 固定表格的列宽
#table.AutoFit(AutoFitBehaviorType.FixedColumnWidths)

# 自动调整表格的宽度以适应窗口大小
#table.AutoFit(AutoFitBehaviorType.AutoFitToWindow)

# 将修改后的文档保存到输出文件中
document.SaveToFile(outputFile)

# 关闭文档对象
document.Close()

Python 设置 Word 文档中的表格格式

Python 设置 Word 文档中表格的对齐方式

Spire.Doc for Python 提供了 Table.TableFormat.HorizontalAlignment 这个属性,用于设置表格的水平对齐方式,例如左对齐、右对齐或居中对齐。下面是详细的步骤:

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

# 设置输出文件路径
outputFile = "output/SetTableAlighment.docx"

# 设置输入文件路径
inputFile = "data/Table.docx"
# 创建文档对象
document = Document()

# 从输入文件中加载文档内容
document.LoadFromFile(inputFile)

# 获取文档的第一个节
section = document.Sections[0]

# 获取第一个节的第一个表格,如果不存在则返回None
table1 = section.Tables[0] if isinstance(section.Tables[0], Table) else None

# 获取第一个节的第二个表格,如果不存在则返回None
table2 = section.Tables[1] if isinstance(section.Tables[1], Table) else None

# 获取第一个节的第三个表格,如果不存在则返回None
table3 = section.Tables[2] if isinstance(section.Tables[2], Table) else None

# 获取第一个节的第四个表格,如果不存在则返回None
table4 = section.Tables[3] if isinstance(section.Tables[3], Table) else None

# 设置第一个表格的水平对齐方式为左对齐
table1.TableFormat.HorizontalAlignment = RowAlignment.Left

# 设置第二个表格的水平对齐方式为居中对齐
table2.TableFormat.HorizontalAlignment = RowAlignment.Center

# 设置第三个表格的水平对齐方式为右对齐
table3.TableFormat.HorizontalAlignment = RowAlignment.Right

# 设置第四个表格的左边距为50
table4.TableFormat.LeftIndent = 50

# 将修改后的文档保存到输出文件中
document.SaveToFile(outputFile, FileFormat.Docx2013)

# 关闭文档对象
document.Close()

Python 设置 Word 文档中的表格格式

Python 设置 Word 文档中表格的边框样式

Spire.Doc for Python 提供了 Table.TableFormat.Borders 这个属性,用于设置表格的边框样式。在 Word 文档中,可以使用这个属性来控制表格的边框线型、宽度和颜色等样式。例如,可以设置边框为实线、虚线或点线,设置边框宽度,以及设置边框的颜色等。下面是详细的步骤:

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

# 设置输出文件路径
outputFile = "output/SetTableFormat.docx"

# 设置输入文件路径
inputFile = "data/Table.docx"

# 创建文档对象
document = Document()

# 从输入文件中加载文档内容
document.LoadFromFile(https://cdn.e-iceblue.cn/inputFile)

# 获取文档的第一个节
section = document.Sections[0]

# 获取第一个节的第一个表格,如果不存在则返回None
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None

# 设置表格右侧边框样式为双线,线宽为1.0,颜色为红色
table.TableFormat.Borders.Right.BorderType = BorderStyle.Double
table.TableFormat.Borders.Right.LineWidth = 1.0
table.TableFormat.Borders.Right.Color = Color.get_Red()

# 设置表格顶部边框样式为双线,线宽为1.0,颜色为绿色
table.TableFormat.Borders.Top.BorderType = BorderStyle.Double
table.TableFormat.Borders.Top.LineWidth = 1.0
table.TableFormat.Borders.Top.Color = Color.get_Green()

# 设置表格左侧边框样式为双线,线宽为1.0,颜色为黄色
table.TableFormat.Borders.Left.BorderType = BorderStyle.Double
table.TableFormat.Borders.Left.LineWidth = 1.0
table.TableFormat.Borders.Left.Color = Color.get_Yellow()

# 设置表格底部边框样式为双线,垂直边框样式为细线,水平边框样式为细线,垂直边框颜色为橙色
table.TableFormat.Borders.Bottom.BorderType = BorderStyle.Double
table.TableFormat.Borders.Vertical.BorderType = BorderStyle.Hairline
table.TableFormat.Borders.Horizontal.BorderType = BorderStyle.Hairline
table.TableFormat.Borders.Vertical.Color = Color.get_Orange()

# 将修改后的文档保存到输出文件路径
document.SaveToFile(outputFile, FileFormat.Docx)

# 关闭文档对象
document.Close()

Python 设置 Word 文档中的表格格式

Python 设置 Word 文档中表格的行或单元格背景颜色

Spire.Doc for Python 提供了 Rows.RowFormat.BackColor 这个属性,用于设置表格指定行的背景颜色。提供了 Cells.CellFormat.BackColor 这个属性,用于设置表格指定行中指定单元格的背景颜色。下面是详细的步骤:

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

# 设置输出文件路径
outputFile = "output/SetTableBackColor.docx"

# 设置输入文件路径
inputFile = "data/Table.docx"

# 创建文档对象
document = Document()

# 从输入文件中加载文档内容
document.LoadFromFile(https://cdn.e-iceblue.cn/inputFile)

# 获取文档的第一个节
section = document.Sections[0]

# 获取第一个节的第一个表格,如果不存在则返回None
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None

#设置第一行的背景颜色
table.Rows[0].RowFormat.BackColor = Color.get_SeaGreen()

#设置第3行第2个单元格的背景颜色
table.Rows[2].Cells[1].CellFormat.BackColor = Color.get_SeaGreen()

# 将修改后的文档保存到输出文件路径
document.SaveToFile(outputFile, FileFormat.Docx)

# 关闭文档对象
document.Close()

Python 设置 Word 文档中的表格格式

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

在 PDF 文件中添加附件的作用是为了增强文件的交互性和功能性。通过添加附件,我们可以将其他文件(如文档、图像、音频或视频)与PDF文件关联起来,并允许用户轻松地访问这些附件。

Spire.PDF for Python 允许您以两种方式附加文件:

本文将介绍如何使用 Spire.PDF for Python 在 Python 中添加或删除 PDF 文档中的附件

安装 Spire.PDF for Python

本教程需要用到 Spire.PDF for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。

pip install Spire.PDF

如果您不确定如何安装,请参考此教程: 如何在 Windows 中安装 Spire.PDF for Python

Python 在 PDF 中添加文档附件

Spire.PDF for Python 提供的 PdfDocument.Attachments.Add() 方法即可轻松地向“附件”面板添加附件。 以下是详细步骤。

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

# 创建一个空的PDF文档对象
doc = PdfDocument()

# 从指定文件加载PDF文档
doc.LoadFromFile("团建活动方案.pdf")

# 创建一个PdfAttachment对象,指定要添加的附件文件为"参与人员名单.xlsx"
attachment = PdfAttachment("参与人员名单.xlsx")

# 将附件添加到PDF文档中
doc.Attachments.Add(attachment)

# 将修改后的PDF文档保存到新的文件中
doc.SaveToFile("添加文档附件.pdf")
doc.Close()

Python 添加或删除 PDF 文档中的附件

Python 在 PDF 中的添加注释附件

注释附件将以图标的形式显示在特定页面上。Spire.PDF for Python 提供的 PdfPageBase.AnnotationsWidget.Add() 方法就是向 PDF 中添加注释附件,以下是详细步骤。

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

# 创建一个PdfDocumet对象
doc = PdfDocument()

# 从指定文件加载一个PDF文档
doc.LoadFromFile("团建活动方案.pdf")

# 获取文档的第一页
page = doc.Pages.get_Item(0)

# 定义画刷为红色
brush = PdfBrushes.get_Red()

# 创建PdfTrueTypeFont字体对象,字体名称为"宋体",大小为18.0,加粗,启用unicode
font = PdfTrueTypeFont("宋体", 18.0, PdfFontStyle.Bold, True)

# 设置绘制文本的起始点坐标xP和yP
xP = float(40)
yP = float(page.Canvas.ClientSize.Height - 80)

# 创建字符串格式对象,设置文本对齐方式为左对齐
format1 = PdfStringFormat(PdfTextAlignment.Left)

# 要绘制的文本内容
label = "在此附上详细安排的附件:"

# 在页面上绘制文本
page.Canvas.DrawString(label, font, brush, xP, yP, format1)

# 计算绘制文本所占宽度
textWidth = font.MeasureString(label).Width

# 创建矩形对象bounds,指定注释图标的位置和大小
bounds = RectangleF(float(xP + textWidth + 5), yP, float(20), float(15))

# 创建流对象data,读取附件文件"详细安排.pptx"
data = Stream("详细安排.pptx")

# 创建PDF附件注释对象annotation,指定图标位置、附件文件名和数据
annotation = PdfAttachmentAnnotation(bounds, "详细安排.pptx", data)

# 设置注释的颜色为橙色
annotation.Color = PdfRGBColor(Color.get_Orange())

# 设置注释为只读
annotation.Flags = PdfAnnotationFlags.ReadOnly

# 设置注释的图标为图表类型
annotation.Icon = PdfAttachmentIcon.Graph

# 设置注释的文本内容
annotation.Text = "双击以打开附件。"

# 将注释对象添加到页面的注释集合中
page.AnnotationsWidget.Add(annotation)

# 将修改后的PDF文档保存到新的文件中
doc.SaveToFile("添加注释附件.pdf")
doc.Close()

Python 添加或删除 PDF 文档中的附件

Python 删除 PDF 中的文档附件

PDF 中的文档附件可以通过 PdfDocument.Attachments 来访问,并且可以使用 PdfAttachmentCollectionRemoveAt(attachmentIndex) 方法来删除特定的附件或 Clear() 方法来删除全部附件。以下步骤详细介绍了如何从 PDF 中删除文档附件:

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

# 创建一个PdfDocument对象
doc = PdfDocument()

# 从指定文件加载一个PDF文档
doc.LoadFromFile("文档附件.pdf")

# 获取PDF文档中的附件对象集合
attachments = doc.Attachments

# 删除PDF文件的第一个附件(这里索引从0开始)
attachments.RemoveAt(0)

# 删除所有附件
# attachments.Clear()

# 将修改后的PDF文档保存到新的文件中
doc.SaveToFile("删除一个附件.pdf")
doc.Close()

Python 删除 PDF 中的注释附件

注释是基于页面的元素。要获取文档中的所有注释,我们必须先遍历文档的所有页面并获取每一个页面中的所有注释,然后判断某个注释是否是注释附件。最后,使用 Remove() 方法从注释集合中删除注释附件。以下步骤详细介绍了如何从 PDF 中删除注释附件:

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

# 创建一个PdfDocument对象
doc = PdfDocument()

# 从指定文件加载一个PDF文档
doc.LoadFromFile("注释附件.pdf")

# 遍历PDF文档中的每一页
for i in range(doc.Pages.Count):
    # 获取当前页
    pageBase = doc.Pages[i]

    # 遍历当前页的注释
    for j in range(pageBase.AnnotationsWidget.Count):
        # 获取当前注释
        annotationWidget = pageBase.AnnotationsWidget[j]

        # 检查注释是否属于PdfAttachmentAnnotationWidget类型
        if isinstance(annotationWidget, PdfAttachmentAnnotationWidget):
            # 从当前页移除该注释
            pageBase.AnnotationsWidget.Remove(annotationWidget)

# 将修改后的PDF文档保存到新的文件中
doc.SaveToFile("删除注释附件.pdf")
doc.Close()

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

Microsoft Excel 中的超链接是一种在电子表格中创建可点击的链接的实用功能。通过添加超链接,你可以在不同的工作表、工作簿、网站,甚至同一工作簿中的特定单元格之间进行导航。无论是需要引用外部资源、链接相关数据还是创建交互式报表,超链接都能帮助您轻松实现目的。本文将演示如何使用 Spire.XLS for Python 在 Python 中为 Excel 添加超链接

安装 Spire.XLS for Python

此教程需要 Spire.XLS for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。

pip install Spire.XLS

如果您不确定如何安装,请参考教程: 如何在 Windows 中安装 Spire.XLS for Python

Python 在 Excel 中添加文本超链接

Excel 中的文本超链接是可点击的词组或短句,可将用户导向 Excel 文件的指定位置、外部资源或电子邮件地址。以下是使用 Python 在 Excel 文件中添加文本超链接的详细步骤:

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

# 创建Workbook类的对象
workbook = Workbook()

# 获取第一张工作表
sheet = workbook.Worksheets[0]

# 添加一个链接到网页的文本超链接
cell1 = sheet.Range["B3"]
urlLink = sheet.HyperLinks.Add(cell1)
urlLink.Type = HyperLinkType.Url
urlLink.TextToDisplay = "链接到网页"
urlLink.Address = "https://www.e-iceblue.cn/"

# 添加一个链接到邮箱的文本超链接
cell2 = sheet.Range["E3"]
mailLink = sheet.HyperLinks.Add(cell2)
mailLink.Type = HyperLinkType.Url
mailLink.TextToDisplay = "链接到邮箱"
mailLink.Address = "mailto:example @outlook.com"

# 添加一个链接到外部文件的文本超链接
cell3 = sheet.Range["B7"]
fileLink = sheet.HyperLinks.Add(cell3)
fileLink.Type = HyperLinkType.File
fileLink.TextToDisplay = "链接到外部文件"
fileLink.Address = "C:\\Users\\Administrator\\Desktop\\报告.xlsx"

# 添加一个链接到指定单元格的文本超链接
cell4 = sheet.Range["E7"]
linkToSheet = sheet.HyperLinks.Add(cell4)
linkToSheet.Type = HyperLinkType.Workbook
linkToSheet.TextToDisplay = "链接到sheet2中的指定单元格"
linkToSheet.Address = "Sheet2!B5"

# 添加一个链接到UNC地址的文本超链接
cell5 = sheet.Range["B11"]
uncLink = sheet.HyperLinks.Add(cell5)
uncLink.Type = HyperLinkType.Unc
uncLink.TextToDisplay = "链接到 UNC 地址"
uncLink.Address = "\\\\192.168.0.121"

# 自适应列宽
sheet.AutoFitColumn(2)
sheet.AutoFitColumn(5)

# 保存结果文件
workbook.SaveToFile("添加文本超链接.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python 在 Excel 中添加超链接

Python 在 Excel 中添加图片超链接

图片超链接即使用图片作为可点击元素,提供了一种更具有视觉吸引力的展示方式,可用于在 Excel 中导航或访问外部资源。以下是使用 Python 在 Excel 文件中添加图片超链接的详细步骤:

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

# 创建Workbook类的对象
workbook = Workbook()

# 获取第一张工作表
sheet = workbook.Worksheets[0]

# 在指定单元格添加文本
sheet.Range["B1"].Text = "图片超链接"
# 设置第二行的行宽
sheet.Columns[1].ColumnWidth = 15

# 插入图片
picture = sheet.Pictures.Add(3, 2, "logo1.jpg")

# 为图片添加超链接
picture.SetHyperLink("https://www.e-iceblue.cn/", True)
            
# 保存结果文件
workbook.SaveToFile("添加图片超链接.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

Python 在 Excel 中添加超链接

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

Spire.Office for Java 8.12.0已发布。在该版本中,Spire.XLS for Java 新增worksheet.getCellImages()方法来获取用WPS工具添加的内嵌图片;Spire.PDF for Java增强了 PDF 到 SVG、PDF/A1B 和 PDF/A2A 的转换功能 。此外,一些已知问题也在该版本中得到修复。详情请阅读以下内容。

获取 Spire.Office for Java 8.12.0请点击:

https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html


Spire.XLS for Java

新功能:

问题修复:


Spire.PDF for Java

问题修复:

超链接在 Word 文档中起到了提供交互性和导航功能的作用,使读者能够方便地浏览相关内容,并且促进了文档的易读性和互动性。本文将介绍如何使用 Spire.Doc for Python 在 Python 中添加和删除 Word 文档中的超链接。

安装 Spire.Doc for Python

本教程需要使用 Spire.Doc for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。

pip install Spire.Doc

如果您不确定如何安装,请参考此教程:如何在 Windows 中安装 Spire.Doc for Python

Python 添加超链接到 Word 文档

Spire.Doc for Python 提供了Paragraph.AppendHyperlink() 方法,可以将网页链接、电子邮件链接、文件链接或书签链接添加到段落内的文本或图像中。下面是详细的步骤:

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

# 创建一个文档对象
doc = Document()

# 添加一个章节
section = doc.AddSection()

# 创建字符格式对象
characterFormat = CharacterFormat(doc)

# 设置字体为宋体
characterFormat.FontName = "宋体"

# 设置字号为12
characterFormat.FontSize = 12

# 添加段落
paragraph = section.AddParagraph()

# 在段落中添加网页链接
field = paragraph.AppendHyperlink("https://www.e-iceblue.cn/", "网站主页", HyperlinkType.WebLink)
# 应用字符格式到超链接
field.ApplyCharacterFormat(characterFormat)

# 在段落中添加换行符
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)

# 在段落中添加邮件链接
field = paragraph.AppendHyperlink("mailto:support @e-iceblue.com", "发邮件给我们", HyperlinkType.EMailLink)
# 应用字符格式到超链接
field.ApplyCharacterFormat(characterFormat)

paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)

# 定义文件路径
filePath = "report.xlsx"
# 在段落中添加文件链接
field = paragraph.AppendHyperlink(filePath, "单击来打开一个Excel报告", HyperlinkType.FileLink)
# 应用字符格式到超链接
field.ApplyCharacterFormat(characterFormat)

# 在段落中添加换行符
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)

# 添加第二个章节
section2 = doc.AddSection()

# 添加段落并插入书签
bookmarkParagrapg = section2.AddParagraph()
bookmarkParagrapg.AppendText("一个书签")
start = bookmarkParagrapg.AppendBookmarkStart("myBookmark")
bookmarkParagrapg.Items.Insert(0, start)
bookmarkParagrapg.AppendBookmarkEnd("myBookmark")

# 在段落中添加链接,跳转到文档内的书签位置
field = paragraph.AppendHyperlink("myBookmark", "跳转到该文档中的一个书签位置", HyperlinkType.Bookmark)
# 应用字符格式到超链接
field.ApplyCharacterFormat(characterFormat)

# 在段落中添加换行符
paragraph.AppendBreak(BreakType.LineBreak)
paragraph.AppendBreak(BreakType.LineBreak)

# 定义图片路径
image = "logo.png"
# 在段落中插入图片
picture = paragraph.AppendPicture(image)
# 在段落中添加图片的网页链接
field = paragraph.AppendHyperlink("https://www.e-iceblue.cn/", picture, HyperlinkType.WebLink)
# 应用字符格式到超链接
field.ApplyCharacterFormat(characterFormat)

# 保存文档
doc.SaveToFile("添加超链接.docx", FileFormat.Docx2016)

# 关闭文档
doc.Close()

# 释放资源
doc.Dispose()

Python 添加和删除 Word 中的超链接

Python 从 Word 文档中删除超链接

要一次性删除 Word 文档中的所有超链接,您需要找到文档中的所有超链接,然后创建一个名为 FlattenHyperlinks() 的自定义方法来依次删除。以下是详细步骤:

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

# 定义函数 FindAllHyperlinks,查找文档中的所有超链接
def FindAllHyperlinks(document):
    # 存储超链接列表的变量
    hyperlinks = []
    for i in range(document.Sections.Count):
        # 获取当前节
        section = document.Sections.get_Item(i)
        for j in range(section.Body.ChildObjects.Count):
            # 获取当前节中的子对象
            sec = section.Body.ChildObjects.get_Item(j)
              # 判断子对象是否为段落
            if sec.DocumentObjectType == DocumentObjectType.Paragraph:
                for k in range((sec if isinstance(sec, Paragraph) else None).ChildObjects.Count):
                    # 获取段落中的子对象
                    para = (sec if isinstance(sec, Paragraph)
                            else None).ChildObjects.get_Item(k)
                      # 判断子对象是否为域
                    if para.DocumentObjectType == DocumentObjectType.Field:
                         # 将子对象转换为域类型
                        field = para if isinstance(para, Field) else None
                        # 判断域类型是否为超链接
                        if field.Type == FieldType.FieldHyperlink:
                            # 将超链接对象添加到列表中
                            hyperlinks.append(field)
    # 返回超链接列表
    return hyperlinks

# 定义函数 FlattenHyperlinks,移除超链接
def FlattenHyperlinks(field):
     # 获取超链接所属段落在文本主体中的索引
    ownerParaIndex = field.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(
        field.OwnerParagraph)
     # 获取超链接在所属段落中的索引
    fieldIndex = field.OwnerParagraph.ChildObjects.IndexOf(field)
     # 获取超链接分隔符所在段落
    sepOwnerPara = field.Separator.OwnerParagraph
     # 获取超链接分隔符所在段落在文本主体中的索引
    sepOwnerParaIndex = field.Separator.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(
        field.Separator.OwnerParagraph)
     # 获取超链接分隔符在所在段落中的索引
    sepIndex = field.Separator.OwnerParagraph.ChildObjects.IndexOf(
        field.Separator)
     # 获取超链接结束符在所属段落中的索引
    endIndex = field.End.OwnerParagraph.ChildObjects.IndexOf(field.End)
     # 获取超链接结束符所在段落在文本主体中的索引
    endOwnerParaIndex = field.End.OwnerParagraph.OwnerTextBody.ChildObjects.IndexOf(
        field.End.OwnerParagraph)

    FormatFieldResultText(field.Separator.OwnerParagraph.OwnerTextBody,
                           sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex)
     # 删除超链接结束符
    field.End.OwnerParagraph.ChildObjects.RemoveAt(endIndex)

    for i in range(sepOwnerParaIndex, ownerParaIndex - 1, -1):
        if i == sepOwnerParaIndex and i == ownerParaIndex:
            for j in range(sepIndex, fieldIndex - 1, -1):
                 # 删除超链接所在段落中的对象
                field.OwnerParagraph.ChildObjects.RemoveAt(j)

        elif i == ownerParaIndex:
            for j in range(field.OwnerParagraph.ChildObjects.Count - 1, fieldIndex - 1, -1):
                 # 删除超链接所在段落中的对象
                field.OwnerParagraph.ChildObjects.RemoveAt(j)

        elif i == sepOwnerParaIndex:
            for j in range(sepIndex, -1, -1):
                 # 删除分隔符所在段落中的对象
                sepOwnerPara.ChildObjects.RemoveAt(j)
        else:
                # 删除超链接所属段落所在文本主体中的对象
            field.OwnerParagraph.OwnerTextBody.ChildObjects.RemoveAt(i)

# 定义函数 FormatFieldResultText,将超链接对象转换为文本并清除文本格式
def FormatFieldResultText(ownerBody, sepOwnerParaIndex, endOwnerParaIndex, sepIndex, endIndex):
    for i in range(sepOwnerParaIndex, endOwnerParaIndex + 1):
        para = ownerBody.ChildObjects[i] if isinstance(
            ownerBody.ChildObjects[i], Paragraph) else None
        if i == sepOwnerParaIndex and i == endOwnerParaIndex:
            for j in range(sepIndex + 1, endIndex):
               if isinstance(para.ChildObjects[j], TextRange):
                 FormatText(para.ChildObjects[j])

        elif i == sepOwnerParaIndex:
            for j in range(sepIndex + 1, para.ChildObjects.Count):
                if isinstance(para.ChildObjects[j], TextRange):
                  FormatText(para.ChildObjects[j])
        elif i == endOwnerParaIndex:
            for j in range(0, endIndex):
               if isinstance(para.ChildObjects[j], TextRange):
                 FormatText(para.ChildObjects[j])
        else:
            for j, unusedItem in enumerate(para.ChildObjects):
                if isinstance(para.ChildObjects[j], TextRange):
                  FormatText(para.ChildObjects[j])

# 格式化文本
def FormatText(tr):
    tr.CharacterFormat.TextColor = Color.get_Black()
    tr.CharacterFormat.UnderlineStyle = UnderlineStyle.none

# 创建一个文档对象
doc = Document()

# 加载一个Word文档
doc.LoadFromFile("示例文档.docx")

# 获取所有的超链接
hyperlinks = FindAllHyperlinks(doc)

# 删除所有的超链接
for i in range(len(hyperlinks) - 1, -1, -1):
    FlattenHyperlinks(hyperlinks[i])

# 保存到一个新Word文档
doc.SaveToFile("删除超链接.docx", FileFormat.Docx2016)

# 关闭文档
doc.Close()

# 释放资源
doc.Dispose()

Python 添加和删除 Word 中的超链接

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

在处理 PDF 文件时,设置密码或从加密的PDF文档中移除密码的功能对于确保敏感信息的安全性,并同时保持使用 PDF 文件的灵活性和便利性至关重要。通过为 PDF 文档设置密码,个人可以控制对其文件的访问权限,防止未经授权的查看、编辑或复制。相反,解除保护 PDF 文档可以使文档重新可访问或可编辑。在本文中,您将学习如何使用 Spire.PDF for Python对 PDF 文档进行密码保护,以及如何从加密的 PDF 文档中移除密码

安装 Spire.PDF for Python

本教程需要 Spire.PDF for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。

pip install Spire.PDF

如果您不确定如何安装,请参考此教程: 如何在 Windows 中安装 Spire.PDF for Python

Python 对 PDF 文档进行密码保护

有两种用于安全目的的密码类型可供选择: "打开密码" 和 "权限密码"。打开密码,也称为用户密码,用于限制未经授权访问 PDF 文件。而权限密码,也被称为主密码或所有者密码,允许您对其他人能够在 PDF 文件中执行的操作设置各种限制。如果一个 PDF 文件同时使用这两种密码进行保护,那么可以使用任意一种密码来打开该文件。

Spire.PDF for Python 提供了一个 PdfDocument.Security.Encrypt(String openPassword, String permissionPassword, PdfPermissionsFlags permissions, PdfEncryptionKeySize keySize) 方法,使您可以使用打开密码和/或权限密码来保护 PDF 文件。其中,PdfPermissionsFlags 参数用于指定用户对文档的操作权限。

以下是使用 Spire.PDF for Python 实现 PDF 密码保护的步骤:

  • Python
from spire.pdf import *

# 创建一个 PdfDocument 对象
doc = PdfDocument()

# 从指定路径加载示例 PDF 文件
doc.LoadFromFile("示例.pdf")

# 使用打开密码 ("openPsd")、权限密码 ("permissionPsd") 和允许打印权限对 PDF 文件进行加密
doc.Security.Encrypt("openPsd", "permissionPsd", PdfPermissionsFlags.Print, PdfEncryptionKeySize.Key128Bit)

# 将加密后的 PDF 文件保存到指定的文件路径
doc.SaveToFile("加密文档.pdf", FileFormat.PDF)

# 关闭文档
doc.Close()

Python 加密或解密 PDF 文档

Python 从加密的 PDF 文档中移除密码

要从 PDF 文件中移除密码,可以调用 PdfDocument.Security.Encrypt() 方法,并将打开密码和权限密码设为空字符串。以下是详细步骤:

  • Python
from spire.pdf import *

# 创建一个 PdfDocument 对象
doc = PdfDocument()

# 使用 "openPsd" 打开密码加载加密的 PDF 文件
doc.LoadFromFile("加密文档.pdf", "openPsd")

# 通过将打开密码和权限密码设为空字符串
doc.Security.Encrypt(str(), str(), PdfPermissionsFlags.Default, PdfEncryptionKeySize.Key128Bit, "permissionPsd")

# 将移除密码后的 PDF 文件保存到指定的文件路径
doc.SaveToFile("移除密码.pdf", FileFormat.PDF)

# 关闭文档
doc.Close()

Python 加密或解密 PDF 文档

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

Spire.Office 8.12.2 已发布。在该版本中,Spire.Doc 支持转换 Word 到 PCL 和 PostScript 的文本整形功能;Spire.Presentation 支持将母版页转换为图片; Spire.PDFViewer 支持在WinForm 项目中用Ctrl+滚轮来实现界面缩放的效果。此外,大量已知问题也在该版本中得到修复。详情请阅读以下内容。

该版本涵盖了最新版的 Spire.Doc,Spire.PDF,Spire.XLS,Spire.Email,Spire.DocViewer,Spire.PDFViewer,Spire.Presentation,Spire.Spreadsheet,Spire.OfficeViewer,Spire.Barcode,Spire.DataExport。

版本信息如下:

获取Spire.Office 8.12.2请点击:

https://www.e-iceblue.cn/Downloads/Spire-Office-NET.html


Spire.Doc

新功能:

问题修复:


Spire.Presentation

新功能:

问题修复:


Spire.PDFViewer

新功能:

问题修复:


Spire.PDF

问题修复:


Spire.XLS

问题修复: