在 Word 中创建用户可填写的表单可以提供一种便捷的电子数据采集方式,这尤其适用于需要收集用户信息、反馈或者需要创建交互式文档的场景。通过添加文本字段、复选框、下拉菜单等各种元素,您可以根据具体需求创建适合自己的定制表单。
要在 Word 中创建可填写的表单,可能需要使用以下工具。
- 内容控件:内容控件是 Word 中用于收集用户输入的区域。
- 表格:表格在表单中用于对齐文本和表单字段,并创建可视化的边框和方框。
- 保护:通过保护文档,您可以允许用户填充表单字段,但限制对文档其他部分的更改。
在 Word 中,内容控件可以充当结构化文档的容器,允许用户组织和管理文档内容。Word 2013 提供了十种类型的内容控件。本文将介绍如何使用 Spire.Doc for 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 提供的 StructureDocumentTagInline 类,用于在段落中生成结构化文档标记。利用该类的 SDTProperties 属性和 SDTContent 属性,可以定义当前结构化文档标签的属性和内容。
以下是使用 Python 在 Word 文档中创建可填充表单的详细步骤:
- 创建 Document 类的对象。
- 使用 Document.AddSection() 方法添加一个章节。
- 使用 Section.AddTable() 方法添加表格。
- 使用 TableCell.AddParagraph() 方法在特定表格单元格中添加段落。
- 创建 StructureDocumentTagInline 类的实例,并使用 Paragraph.ChildObjects.Add() 方法将其作为子对象添加到段落中。
- 通过 StructureDocumentTagInline 对象的 SDTProperties 属性和 SDTContent 属性指定结构化文档标记的类型、内容和其他属性。
- 使用 Document.Protect() 方法防止用户编辑表单字段以外的内容。
- 使用 Document.SaveToFile() 方法保存文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建Document对象
doc = Document()
# 添加一节
section = doc.AddSection()
# 添加一个表格
table = section.AddTable(True)
table.ResetCells(7, 2)
table.SetColumnWidth(0, 120, CellWidthType.Point)
table.SetColumnWidth(1, 350, CellWidthType.Point)
# 在第一列的单元格中添加文本
paragraph = table.Rows[0].Cells[0].AddParagraph()
paragraph.AppendText("名字")
paragraph = table.Rows[1].Cells[0].AddParagraph()
paragraph.AppendText("信息")
paragraph = table.Rows[2].Cells[0].AddParagraph()
paragraph.AppendText("照片")
paragraph = table.Rows[3].Cells[0].AddParagraph()
paragraph.AppendText("国家")
paragraph = table.Rows[4].Cells[0].AddParagraph()
paragraph.AppendText("爱好")
paragraph = table.Rows[5].Cells[0].AddParagraph()
paragraph.AppendText("生日")
paragraph = table.Rows[6].Cells[0].AddParagraph()
paragraph.AppendText("性别")
# 在单元格(0,1)中添加纯文本内容控件
paragraph = table.Rows[0].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.Text
sdt.SDTProperties.Alias = "纯文本内容控件"
sdt.SDTProperties.Tag = "纯文本内容控件"
sdt.SDTProperties.IsShowingPlaceHolder = True
text = SdtText(True)
text.IsMultiline = False
sdt.SDTProperties.ControlProperties = text
textRange = TextRange(doc)
textRange.Text = "请填写姓名"
sdt.SDTContent.ChildObjects.Add(textRange)
# 在单元格(1,1)中添加格式文本内容控件
paragraph = table.Rows[1].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.RichText
sdt.SDTProperties.Alias = "格式文本内容控件"
sdt.SDTProperties.Tag = "格式文本内容控件"
sdt.SDTProperties.IsShowingPlaceHolder = True
text = SdtText(True)
text.IsMultiline = False
sdt.SDTProperties.ControlProperties = text
textRange = TextRange(doc)
textRange.Text = "自我简介"
sdt.SDTContent.ChildObjects.Add(textRange )
# 在单元格(2,1)中添加图片内容控件
paragraph = table.Rows[2].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.Picture
sdt.SDTProperties.Alias = "图片内容控件"
sdt.SDTProperties.Tag = "图片内容控件"
sdtPicture = SdtPicture(True)
sdt.SDTProperties.ControlProperties = sdtPicture
pic = DocPicture(doc)
pic.LoadImage("上传.jpg")
sdt.SDTContent.ChildObjects.Add(pic)
# 在单元格 (3,1) 中添加下拉列表内容控件
paragraph = table.Rows[3].Cells[1].AddParagraph();
sdt = StructureDocumentTagInline(doc)
sdt.SDTProperties.SDTType = SdtType.DropDownList
sdt.SDTProperties.Alias = "下拉列表内容控件"
sdt.SDTProperties.Tag = "下拉列表内容控件"
paragraph.ChildObjects.Add(sdt)
stdList = SdtDropDownList()
stdList.ListItems.Add(SdtListItem("中国", "1"))
stdList.ListItems.Add(SdtListItem("美国", "2"))
stdList.ListItems.Add(SdtListItem("英国", "3"))
stdList.ListItems.Add(SdtListItem("法国", "4"))
sdt.SDTProperties.ControlProperties = stdList;
textRange = TextRange(doc)
textRange .Text = stdList.ListItems[0].DisplayText
sdt.SDTContent.ChildObjects.Add(textRange )
# 在单元格 (4,1) 中添加两个复选框内容控件
paragraph = table.Rows[4].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.CheckBox
sdtCheckBox = SdtCheckBox()
sdt.SDTProperties.ControlProperties = sdtCheckBox
textRange = TextRange(doc)
sdt.ChildObjects.Add(textRange)
sdtCheckBox.Checked = False
paragraph.AppendText(" 电影")
paragraph = table.Rows[4].Cells[1].AddParagraph();
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.CheckBox
sdtCheckBox = SdtCheckBox()
sdt.SDTProperties.ControlProperties = sdtCheckBox
textRange = TextRange(doc)
sdt.ChildObjects.Add(textRange)
sdtCheckBox.Checked = False
paragraph.AppendText(" 游戏")
# 在单元格 (5,1) 中添加日期选择器内容控件
paragraph = table.Rows[5].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.DatePicker
sdt.SDTProperties.Alias = "日期选取器内容控件"
sdt.SDTProperties.Tag = "日期选取器内容控件"
stdDate = SdtDate()
stdDate.CalendarType = CalendarType.Default
stdDate.DateFormat = "yyyy.MM.dd"
stdDate.FullDate = DateTime.get_Now()
sdt.SDTProperties.ControlProperties = stdDate
textRange = TextRange(doc)
textRange.Text = "你的出生日期"
sdt.SDTContent.ChildObjects.Add(textRange)
# 在单元格 (6,1) 中添加组合框内容控件
paragraph = table.Rows[6].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.ComboBox
sdt.SDTProperties.Alias = "组合框内容控件"
sdt.SDTProperties.Tag = "组合框内容控件"
stdComboBox = SdtComboBox()
stdComboBox.ListItems.Add(SdtListItem("男性"))
stdComboBox.ListItems.Add(SdtListItem("女性"))
sdt.SDTProperties.ControlProperties = stdComboBox
textRange = TextRange(doc)
textRange.Text = stdComboBox.ListItems[0].DisplayText
sdt.SDTContent.ChildObjects.Add(textRange)
# 仅允许用户编辑表格字段
doc.Protect(ProtectionType.AllowOnlyFormFields, "123abc")
# 保存结果文件
doc.SaveToFile("Word表单.docx", FileFormat.Docx2013)
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。