You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
919 B
32 lines
919 B
9 months ago
|
from docx import Document
|
||
|
|
||
|
# 打开目标Word文档
|
||
|
document = Document('数据库概要设计说明书.docx')
|
||
|
# 打开样板文件
|
||
|
doc = Document('temp.docx')
|
||
|
database_index = 0
|
||
|
table_index = 1
|
||
|
empty_table = False
|
||
|
|
||
|
# 遍历文档中的所有段落
|
||
|
for paragraph in document.paragraphs:
|
||
|
if paragraph.text.startswith('数据库:'):
|
||
|
database_index += 1
|
||
|
doc.add_heading(str(database_index) + "." + paragraph.text, 1)
|
||
|
table_index = 1
|
||
|
elif paragraph.text.startswith('表名:'):
|
||
|
if len(paragraph.text) == 3:
|
||
|
empty_table = True
|
||
|
continue
|
||
|
else:
|
||
|
empty_table = False
|
||
|
doc.add_heading(str(database_index) + "." + str(table_index) + "." + paragraph.text, 2)
|
||
|
table_index += 1
|
||
|
elif empty_table:
|
||
|
continue
|
||
|
else:
|
||
|
doc.add_paragraph(paragraph.text)
|
||
|
|
||
|
# 保存修改后的文档
|
||
|
doc.save('modified_document.docx')
|