Python 如何开发,以便以有组织的方式将下列字段转换为MS WORD文件?
字段: A。 带掩码和 15个字符 B。 棍棒名称必须是选择按钮和带有 3个选项 C。 部件名称必须是的文本字段 免费灌装Python 如何开发,以便以有组织的方式将下列字段转换为MS WORD文件?,python,python-docx,Python,Python Docx,字段: A。 带掩码和 15个字符 B。 棍棒名称必须是选择按钮和带有 3个选项 C。 部件名称必须是的文本字段 免费灌装 如果尚未安装python docx,请运行以下pip命令进行安装: pip install python-docx 创建新的python文件并导入docx import docx 您现在可以执行以下操作: MyDocument = docx.Document # Tells python the file extension to write to MyDocumen
如果尚未安装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文件扩展名下创建一个段落,添加一个示例段落和一个标题,并将其保存到给定名称。只要给定句柄、要添加的段落和保存路径,就可以添加任意数量的段落
您还可以创建一个run,通过为某个段落指定句柄,将该段落置于该段落下:
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
如果您需要查找其他内容,这里有一个您的组织方式规范太宽泛了。展示你的尝试并提出一个更集中的问题。你问题的最后一段是毫无意义的胡言乱语。这应该是某种模板吗?@TomServo抱歉。不是我提出的问题。它是发给我的。这只是一个练习。在我完成后,他们希望我创建一个可执行文件并发送给他们。你认为这很复杂吗?但回答你的问题,我认为他们想要一种模板,因为它被发送给了他们你在这里问得不好。把它变成一个高质量的问题。阅读[