如何进行开发以便将以下指示的字段以有组织的方式转换为 MS WORD 文件?
How to develop so that the fields below indicated are transformed into an MS WORD file, in an organized manner?
字段:
一个。
进程号(带掩码和
15 个字符)
乙。
棒名称(必须是选择按钮并列出
3 个选项)
C。
部件名称(必须是
免费填充)
如果您尚未安装 python-docx,运行 使用以下 pip 命令进行安装:
pip install python-docx
创建一个新的 python 文件并导入 docx
import docx
您现在可以执行以下操作:
MyDocument = docx.Document # Tells python the file extension to write to
MyDocument.add_heading("This is a heading.", 0) # Makes a heading. Replace '0' with 1 or 2 to make it smaller
MyDocument.add_paragraph("Hey, I'm a paragraph!") # Makes a paragraph
MyDocument.save("C:/WordDocumentTest.docx") # Saves the document to its respective name, replace C:/ with a directory path
这将在 .docx 文件扩展名下创建一个段落,添加一个示例段落、一个标题,并将其保存为给定的名称。只要给定句柄、要添加的段落和保存路径,就可以添加任意多的段落。
您还可以创建 运行 通过给定句柄在给定段落下放置一个部分:
secondParagraph = MyDocument.add_paragraph("I am the second paragraph!")
secondParagraph.add_run("This is a run added as a section of the second paragraph")
这是我对如何实现您想要的目标的最佳回答:
from docx import Document
from docx.shared import Inches
MyDocument = docx.Document # Tells python the file extension to write to
MyDocument.add_heading('Process Number,', level=1)
MyDocument.add_paragraph('')
MyDocument.add_heading('Stick Name', level=1)
MyDocument.add_paragraph('List Option 1', style='List Number')
MyDocument.add_paragraph('List Option 2', style='List Number')
MyDocument.add_paragraph('List Option 3', style='List Number')
records = ((3, 'Sample'),(7, 'Sample'))
table = MyDocument.add_table(rows=1, cols=2)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Part Name'
hdr_cells[1].text = ' '
for name, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = ''
row_cells[1].text = ''
MyDocument.save("C:/WordDocumentTest.docx") # Saves the document to its respective name, replace C:/ with a directory path
如果您需要查找其他内容,请点击此处 documentation
字段:
一个。 进程号(带掩码和 15 个字符)
乙。 棒名称(必须是选择按钮并列出 3 个选项)
C。 部件名称(必须是 免费填充)
如果您尚未安装 python-docx,运行 使用以下 pip 命令进行安装:
pip install python-docx
创建一个新的 python 文件并导入 docx
import docx
您现在可以执行以下操作:
MyDocument = docx.Document # Tells python the file extension to write to
MyDocument.add_heading("This is a heading.", 0) # Makes a heading. Replace '0' with 1 or 2 to make it smaller
MyDocument.add_paragraph("Hey, I'm a paragraph!") # Makes a paragraph
MyDocument.save("C:/WordDocumentTest.docx") # Saves the document to its respective name, replace C:/ with a directory path
这将在 .docx 文件扩展名下创建一个段落,添加一个示例段落、一个标题,并将其保存为给定的名称。只要给定句柄、要添加的段落和保存路径,就可以添加任意多的段落。
您还可以创建 运行 通过给定句柄在给定段落下放置一个部分:
secondParagraph = MyDocument.add_paragraph("I am the second paragraph!")
secondParagraph.add_run("This is a run added as a section of the second paragraph")
这是我对如何实现您想要的目标的最佳回答:
from docx import Document
from docx.shared import Inches
MyDocument = docx.Document # Tells python the file extension to write to
MyDocument.add_heading('Process Number,', level=1)
MyDocument.add_paragraph('')
MyDocument.add_heading('Stick Name', level=1)
MyDocument.add_paragraph('List Option 1', style='List Number')
MyDocument.add_paragraph('List Option 2', style='List Number')
MyDocument.add_paragraph('List Option 3', style='List Number')
records = ((3, 'Sample'),(7, 'Sample'))
table = MyDocument.add_table(rows=1, cols=2)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Part Name'
hdr_cells[1].text = ' '
for name, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = ''
row_cells[1].text = ''
MyDocument.save("C:/WordDocumentTest.docx") # Saves the document to its respective name, replace C:/ with a directory path
如果您需要查找其他内容,请点击此处 documentation