Spire.Office for Java 9.8.0 现已正式发布。最新版本新增了一些功能,如 Spire.XLS for Java 支持嵌入图片到单元格。同时,Spire.Doc for Java 提高了将 Word 文档转换为 HTML 时图片的清晰度。此外,一些已知问题也在该版本中被成功修复。详情请阅读以下内容。
获取 Spire.Office for Java 9.8.0 请点击:https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html
新功能:
Chart chart = sheet.getCharts().get(0);
String[] labels = chart.getCategoryLabels();
chart.hideCategoryLabels(new String[]
{ labels [0], labels [1], ...}
); / /需要隐藏的类别标签在labels里面自行选取worksheet.getCellRange("B1").insertOrUpdateCellImage("D:\\vs1.png",true);问题修复:
问题修复:
优化:
问题修复:
表单域及其相关的脚本或动作可能会增加 PDF 文件的大小,尤其是在大型文档中,多个表单域会显著影响文件的加载和处理速度。通过删除不必要的表单域,可以有效减小文件体积,提升文档的打开和浏览速度,从而优化用户体验。这篇文章将介绍如何使用 Spire.PDF for Python 和 Python 删除 PDF 中的表单域。
本教程需要用到 Spire.PDF for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.PDF
如果您不清楚如何安装,请参考此教程: 如何在 Windows 中安装 Spire.PDF for Python
Spire.PDF for Python 支持通过表单域的索引或名称来删除 PDF 文件中的特定表单域。以下是具体步骤:
from spire.pdf.common import *
from spire.pdf import *
# 创建 PdfDocument 实例
doc = PdfDocument()
# 加载 PDF 文件
doc.LoadFromFile("表单.pdf")
# 获取文档中的表单域集合
pdfForm = doc.Form
formWidget = PdfFormWidget(pdfForm)
# 根据索引删除集合中的第一个表单域
formWidget.FieldsWidget.RemoveAt(0)
# 或者通过表单域的名称删除表单域
# text_box = formWidget.FieldsWidget["姓名"]
# formWidget.FieldsWidget.Remove(text_box)
# 保存结果文档
doc.SaveToFile("删除特定表单.pdf")
doc.Close()

要删除 PDF 文档中的所有表单域,你需要从后往前遍历表单域集合,然后使用 PdfFormWidget.FieldsWidget.RemoveAt(index) 方法逐一删除每个表单域。以下是具体步骤:
from spire.pdf.common import *
from spire.pdf import *
# 创建 PdfDocument 实例
doc = PdfDocument()
# 加载 PDF 文件
doc.LoadFromFile("表单.pdf")
# 获取文档中的表单域集合
pdfForm = doc.Form
formWidget = PdfFormWidget(pdfForm)
# 检查集合中是否有表单域
if formWidget.FieldsWidget.Count > 0:
# 从后往前遍历所有表单域
for i in range(formWidget.FieldsWidget.Count - 1, -1, -1):
# 删除表单域
formWidget.FieldsWidget.RemoveAt(i)
# 保存结果文档
doc.SaveToFile("删除所有表单.pdf")
doc.Close()

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Cloud 9.8.2 已发布。本次更新新增 PPT AI 功能,支持通过自然语言指令帮助用户生成 PPT、生成图片、编辑格式、翻译文本、操作幻灯片等,简化 PPT 创作过程。该版本还增加了转换文档为 Markdown 格式的接口。详情请阅读以下内容。
新功能:
Spire.Doc 12.8.12 现已发布。 此次更新主要优化了 Word 转 OFD 的文件大小,并修复了一个在替换文本后出现的问题。具体修复内容如下。
问题修复:
数字签名在确保 PDF 文档的真实性和完整性方面发挥着至关重要的作用。它们不仅能可靠地验证签署人的身份,还能确保文档在签名后未被篡改。利用数字签名,你可以显著提升文档的安全性和可信度。在本文中,我们将介绍如何使用 Spire.PDF for Python 和 Python 在 PDF 文件中添加和删除数字签名。
本教程需要 Spire.PDF for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.PDF
如果您不确定如何安装,请参考此教程: 如何在 Windows 中安装 Spire.PDF for Python
你可以使用 Spire.PDF for Python 提供的 PdfOrdinarySignatureMaker.MakeSignature(sigFieldName: str, page: PdfPageBase, x: float, y: float, width: float, height: float, signatureAppearance: IPdfSignatureAppearance) 方法给 PDF 文档的特定页面添加可见数字签名。详细步骤如下:
from spire.pdf.common import *
from spire.pdf import *
# 创建 PdfDocument 实例
doc = PdfDocument()
# 加载PDF文件
doc.LoadFromFile("测试.pdf")
# 创建 PdfOrdinarySignatureMaker 实例
signatureMaker = PdfOrdinarySignatureMaker(doc, "gary.pfx", "e-iceblue")
# 设置签名属性,如签署人的姓名、联系信息、位置和签名原因
signature = signatureMaker.Signature
signature.Name = "Gary"
signature.ContactInfo = "+86 12345678"
signature.Location = "中国"
signature.Reason = "我是文档作者"
# 创建 PdfSignatureAppearance 实例
appearance = PdfSignatureAppearance(signature)
# 为“签署人姓名”设置标签
appearance.NameLabel = "签署人:"
# 为“联系信息”设置标签
appearance.ContactInfoLabel = "电话:"
# 为“位置”设置标签
appearance.LocationLabel = "地址:"
# 为“签名原因”设置标签
appearance.ReasonLabel = "原因:"
# 设置签名图片
appearance.SignatureImage = PdfImage.FromFile("SigImg.png")
# 设置签名的图形渲染/显示模式(仅显示签名图片)
appearance.GraphicMode = GraphicMode.SignImageOnly
# 设置签名图片的布局
appearance.SignImageLayout = SignImageLayout.none
# 获取第一页
page = doc.Pages[0]
# 将签名添加到页面的指定位置
signatureMaker.MakeSignature("签名", page, 90.0, 650.0, 260.0, 100.0, appearance)
# 保存已签名的文档
doc.SaveToFile("数字签名.pdf")
doc.Close()

PDF 中的不可见签名是一种特殊的数字签名,它提供了与可见数字签名相同的安全性,但在文档本身中不可见。使用 Spire.PDF for Python 的 PdfOrdinarySignatureMaker.MakeSignature(sigFieldName: str) 方法,你可以将不可见数字签名添加到 PDF 文档中。详细步骤如下:
from spire.pdf.common import *
from spire.pdf import *
# 创建 PdfDocument 实例
doc = PdfDocument()
# 加载PDF文档
doc.LoadFromFile("测试.pdf")
# 创建 PdfOrdinarySignatureMaker 实例
signatureMaker = PdfOrdinarySignatureMaker(doc, "gary.pfx", "e-iceblue")
# 给文档添加不可见数字签名
signatureMaker.MakeSignature("签名")
# 保存已签名的文档
doc.SaveToFile("不可见签名.pdf")
doc.Close()

要从 PDF 文档中删除数字签名,你需要遍历文档中的所有表单域,找到类型为 PdfSignatureFieldWidget 的表单域,然后将其从文档中删除。详细步骤如下:
from spire.pdf.common import *
from spire.pdf import *
# 创建 PdfDocument 实例
doc = PdfDocument()
# 加载PDF文档
doc.LoadFromFile("数字签名.pdf")
# 获取文档中的表单域集合
pdfForm = doc.Form
formWidget = PdfFormWidget(pdfForm)
# 检查集合中是否有表单域
if formWidget.FieldsWidget.Count > 0:
# 从后往前遍历所有表单域
for i in range(formWidget.FieldsWidget.Count - 1, -1, -1):
field = formWidget.FieldsWidget[i]
# 检查域是否为PdfSignatureFieldWidget
if isinstance(field, PdfSignatureFieldWidget):
# 删除域
formWidget.FieldsWidget.RemoveAt(i)
# 保存文档
doc.SaveToFile("删除签名.pdf")
doc.Close()
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.XLS for Java.14.8.2 现已正式发布。最新版本支持入图片到单元格。此外,一些已知问题也在本次更新中被成功修复,比如保存 Excel 文档,在微软 Excel2016 打开提示内容有错误的问题。详情请查阅以下内容。
新功能:
Chart chart = sheet.getCharts().get(0);
String[] labels = chart.getCategoryLabels();
chart.hideCategoryLabels(new String[]
{ labels [0], labels [1], ...}
); / /需要隐藏的类别标签在labels里面自行选取worksheet.getCellRange("B1").insertOrUpdateCellImage("D:\\vs1.png",true);问题修复:
当你需要准备同主题的多个 PowerPoint 演示文稿时,复制幻灯片可以帮助你保持设计风格、排版和内容上的一致性。这保证了所有演示文稿具有统一的外观,从而提升文档的美观性。本文将介绍怎样通过 Spire.Presentation for Python 使用 Python 在 PowerPoint 演示文稿中复制或克隆幻灯片。
本教程需要用到 Spire.Presentation for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Presentation
如果您不确定如何安装,请参考此教程:如何在 Windows 中安装 Spire.Presentation for Python
你可以通过 Presentation.Slides.Insert(Index: int, slide: ISlide) 方法将幻灯片复制到 PowerPoint 演示文稿中的指定位置,或使用 Presentation.Slides.AppendBySlide(slide: ISlide) 方法将幻灯片复制到演示文稿的末尾。下面就是完成这个操作的详细步骤:
from spire.presentation.common import *
from spire.presentation import *
# 创建一个 PowerPoint 实例
ppt = Presentation()
# 加载演示文稿
ppt.LoadFromFile("pre2.pptx")
# 获取演示文稿的第一张幻灯片
slide = ppt.Slides[0]
# 将幻灯片复制到演示文稿的末尾
ppt.Slides.AppendBySlide(slide)
# 将幻灯片复制到第三张的位置
ppt.Slides.Insert(2, slide)
# 保存文档到磁盘
ppt.SaveToFile("复制幻灯片到同一个PPT.pptx", FileFormat.Pptx2016)
# 释放资源
ppt.Dispose()

Spire.Presentation for Python 也支持同时加载两个 PowerPoint 文件,然后将其中一个演示文稿的幻灯片复制到另一个中。下面就是详细的操作步骤:
from spire.presentation.common import *
from spire.presentation import *
# 加载一个 PowerPoint 演示文稿
sourcePPT = Presentation()
sourcePPT.LoadFromFile("pre1.pptx")
# 加载第二个 PowerPoint 演示文稿
destPPT = Presentation()
destPPT.LoadFromFile("pre2.pptx")
# 获取第一个演示文稿的两张幻灯片
slide1 =sourcePPT.Slides[1]
slide2 =sourcePPT.Slides[3]
# 将获取的第一张幻灯片复制到第二个演示文稿的第二的位置
destPPT.Slides.Insert(1, slide1)
# 将获取的第二张幻灯片复制到第二个演示文稿的末尾
destPPT.Slides.AppendBySlide(slide2)
# 保存第二个演示文稿
destPPT.SaveToFile("复制幻灯片到另一个PPT.pptx", FileFormat.Pptx2016)
# 释放资源
destPPT.Dispose()

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
PowerPoint 演示文稿中经常包含超链接,用于引导到更多资源或在演示文稿内部进行导航。虽然这些链接可以为资源访问和导航带来便利,但有时也可能会影响演示的流畅进行,或显得演示文稿不那么专业。对于幻灯片中无效或不必要的链接,我们可以通过 Python 代码轻松删除,提升演示文稿的整体质量。本文将介绍如何使用 Spire.Presentation for Python 批量删除 PowerPoint 演示文稿中的超链接。
本教程需要用到 Spire.Presentation for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Presentation
如果您不确定如何安装,请参考: 如何在 Windows 中安装 Spire.Presentation for Python
PowerPoint 演示文稿中的普通文本存放在各种自动形状中。开发人员可以通过使用 IAutoShape.TextFrame.Paragraphs[].TextRanges[] 属性访问这些形状中的文本范围,并通过 TextRange.ClickAction 属性读取或设置其上的超链接。将 TextRange.ClickAction 属性设置为 None 即可删除这些超链接。
详细步骤如下:
from spire.presentation import Presentation, IAutoShape, FileFormat
# 创建一个 Presentation 类的实例
presentation = Presentation()
# 加载一个 PowerPoint 演示文稿
presentation.LoadFromFile("示例.pptx")
# 遍历演示文稿中的幻灯片
for slide in presentation.Slides:
# 遍历幻灯片中的形状
for shape in slide.Shapes:
# 判断形状是否为 AutoShape 实例
if isinstance(shape, IAutoShape):
# 遍历形状中的段落
for paragraph in shape.TextFrame.Paragraphs:
# 遍历段落中的文本范围
for textRange in paragraph.TextRanges:
# 判断文本范围是否有超链接
if textRange.ClickAction is not None:
# 移除超链接
textRange.ClickAction = None
# 保存演示文稿
presentation.SaveToFile("output/移除演示文稿文本超链接.pptx", FileFormat.Pptx2013)
presentation.Dispose()

IShape 类代表了演示文稿幻灯片中的所有形状类型,如自动形状、图片、表格等。可以通过将 IShape.Click.get_NoAction() 方法返回的值设置为形状的 IShape.Click 属性值来移除所有这些形状上的超链接。详细步骤如下:
from spire.presentation import Presentation, FileFormat
# 创建一个 Presentation 类的实例
presentation = Presentation()
# 加载一个 PowerPoint 演示文稿
presentation.LoadFromFile("示例.pptx")
# 遍历演示文稿中的幻灯片
for slide in presentation.Slides:
# 遍历幻灯片中的形状
for shape in slide.Shapes:
# 判断形状是否有点击动作
if shape.Click is not None:
# 移除点击动作(设置为无动作)
shape.Click = shape.Click.get_NoAction()
# 保存演示文稿
presentation.SaveToFile("output/移除演示文稿形状超链接.pptx", FileFormat.Pptx2013)
presentation.Dispose()

除了直接移除所有形状的超链接外,我们还可以在移除超链接之前判断形状的类型,以查找并删除特定类型形状上的超链接。详细步骤如下:
from spire.presentation import Presentation, FileFormat, IEmbedImage, ITable, IChart
# 创建一个 Presentation 类的实例
presentation = Presentation()
# 加载一个 PowerPoint 演示文稿
presentation.LoadFromFile("示例.pptx")
# 遍历演示文稿中的幻灯片
for slide in presentation.Slides:
# 遍历幻灯片中的形状
for shape in slide.Shapes:
# 判断形状是否为嵌入图像、表格或图表
if isinstance(shape, (IEmbedImage, ITable, IChart)):
# 检查形状是否有点击动作
if shape.Click is not None:
# 移除超链接(设置点击动作为无动作)
shape.Click = shape.Click.get_NoAction()
# 保存演示文稿
presentation.SaveToFile("output/移除演示文稿特定形状超链接.pptx", FileFormat.Pptx2013)
presentation.Dispose()

要删除演示文稿表格内文本的超链接,需要先找出表格类型的形状,然后遍历表格的单元格以及每个单元格中的文本范围。之后,可以通过将 TextRange.ClickAction 属性设置为 None 来删除每个单元格中文本范围上的超链接。详细步骤如下:
from spire.presentation import Presentation, ITable, FileFormat
# 创建一个 Presentation 类的实例
presentation = Presentation()
# 加载一个 PowerPoint 演示文稿
presentation.LoadFromFile("示例.pptx")
# 遍历演示文稿中的幻灯片
for slide in presentation.Slides:
# 遍历幻灯片中的形状
for shape in slide.Shapes:
# 判断形状是否为表格
if isinstance(shape, ITable):
# 获取表格
table = shape
# 遍历表格中的行
for row in table.TableRows:
# 遍历行中的单元格
for cell in row:
# 遍历单元格中的段落
for para in cell.TextFrame.Paragraphs:
# 遍历段落中的文本范围
for text_range in para.TextRanges:
# 判断文本范围是否包含超链接
if text_range.ClickAction is not None:
# 移除超链接
text_range.ClickAction = None
# 保存演示文稿
presentation.SaveToFile("output/移除演示文稿表格文本超链接.pptx", FileFormat.Pptx2013)
presentation.Dispose()

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.PDF for Python 10.8.1 已经正式发布。新版本支持将 imageInfo[i].Image 返回为 byte[] 类型。此外,查找高亮文本,高亮位置不准确的问题也被成功修复。详情请阅读下面的内容。
新功能:
byteResult = imageInfo[i].Image.ToArray()
fileName = outputFile_1 + "Bug_6888_{0:d}.png".format(i)
with open(fileName,'wb') as f:
f.write(byteResult)pdf=PdfDocument()
pdf.LoadFromFile(inputFile)
page = pdf.Pages[0]
textreplacer = PdfTextReplacer(page)
options = PdfTextReplaceOptions()
options.ReplaceType = ReplaceActionType.WholeWord
textreplacer.Options = options
textreplacer.ReplaceAllText("荷塘", "池塘")
pdf.SaveToFile(outputFile)pdf = PdfDocument()
pdf.LoadFromFile(inputFile)
inder = PdfTextFinder(pdf.Pages[0])
fragments = finder.Find("PDF")
sb = []
for fragment in fragments:
sb.append(fragment.TextStates[0].FontName)
sb.append(fragment.TextStates[0].FontFamily)
sb.append(str(round(fragment.TextStates[0].FontSize,2)))
File.AppendAllText(outputFile,sb)
pdf.Close()# Load a Pdf document from disk
doc = PdfDocument()
doc.LoadFromFile(inputFile)
# Create a signature maker with the PDF document and PFX file
signatureMaker = PdfOrdinarySignatureMaker(doc, inputFile_pfx, "e-iceblue")
# Configure the signature properties
signature = signatureMaker.Signature
signature.Name = "Gary"
signature.ContactInfo = "028-81705109"
signature.Location = "Chengdu"
signature.Reason = "The certificate of this document"
# Create a signature appearance
appearance = PdfSignatureAppearance(signature)
appearance.NameLabel = "Signer: "
appearance.ContactInfoLabel = "ContactInfo: "
appearance.LocationLabel = "Location: "
appearance.ReasonLabel = "Reaseon: "
appearance.SignatureImage = PdfImage.FromFile(inputImage)
appearance.GraphicMode = GraphicMode.SignImageAndSignDetail
appearance.SignImageLayout = SignImageLayout.none
# Apply the signature to the PDF document
signatureMaker.MakeSignature("Signer:", doc.Pages.get_Item(0), 90.0, 550.0, 270.0, 90.0, appearance)
# Save the document
doc.SaveToFile(outputFile)问题修复:
水印在许多类型的文档中是常见的设计元素,用于传达所有权、机密性或品牌信息,但 Microsoft Excel 并未提供内置的水印功能。不过,仍然有一些替代方法可以在 Excel 电子表格中实现水印效果。
一种方法是将图片添加到工作表的页眉或页脚,另一种方法是将图片作为背景添加到工作表中。本文将介绍如何使用 Spire.XLS for Python 在 Python 中向 Excel 添加页眉或背景图片水印。
本方案需要 Spire.XLS for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它轻松安装到 Windows 中。
pip install Spire.XLS
如果您不清楚如何安装,请参考此教程: 如何在 Windows 中安装 Spire.XLS for Python
页眉图像水印
优点:
缺点:
背景图像水印
优点:
缺点:
Spire.XLS for Python 提供 PageSetup 类来控制与外观和打印工作表的排版有关的各种设置。在这个类里,你可以找到 CenterHeader 和 CenterHeaderImage 属性,它们可以用来在页眉中间位置设置图像。
下面是使用 Python 添加页眉图像水印到 Excel 的步骤:
from spire.xls import *
from spire.xls.common import *
# 创建一个 Workbook 对象
workbook = Workbook()
# 加载 Excel 文件
workbook.LoadFromFile("发票.xlsx")
# 加载图片
stream = Stream("confidential.png")
# 遍历文件中的所有工作表
for i in range(workbook.Worksheets.Count):
# 获取特定工作表
worksheet = workbook.Worksheets[i]
# 在页眉中心添加图片字段
worksheet.PageSetup.CenterHeader = "&G"
# 添加图片到页眉中心
worksheet.PageSetup.CenterHeaderImage = stream
# 保存结果文件
workbook.SaveToFile("添加页眉水印.xlsx", ExcelVersion.Version2016)
# 释放资源
workbook.Dispose()

PageSetup 类提供了 BackgroundImage 属性,用于获取或设置背景图片。以下是使用 Python 向 Excel 添加背景图片水印的步骤。
from spire.xls import *
from spire.xls.common import *
# 创建一个 Workbook 对象
workbook = Workbook()
# 加载 Excel 文件
workbook.LoadFromFile("发票.xlsx")
# 加载图片
stream = Stream("sample.png")
# 遍历文件中的所有工作表
for i in range(workbook.Worksheets.Count):
# 获取特定工作表
worksheet = workbook.Worksheets[i]
# 将图片设置为工作表的背景
worksheet.PageSetup.BackgoundImage = stream
# 保存修改后的文档为新的 Excel 文档
workbook.SaveToFile("添加背景水印.xlsx", ExcelVersion.Version2016)
# 释放资源
workbook.Dispose()

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