from docx import Document
from openpyxl import load_workbook
# 利用os模块建立文件夹,用于存放生成的合同
import os
# 给定合同模板和汇总表所在的文件夹路径,方便复用
path = r'C:\Users\james\Desktop\合同数据'
# 结合路径判断生成文件夹,规避程序报错而终止的风险
if not os.path.exists(path + '/' + '全部合同'):
os.mkdir(path + '/' + '全部合同')
workbook = load_workbook(path + '/' + '合同信息表.xlsx')
sheet = workbook.active
for table_row in range(2, sheet.max_row + 1):
wordfile = Document(path + '/' + '合同模板.docx') #打开合同模板
for table_col in range(1, sheet.max_column + 1):
old_text = str(sheet.cell(row=1, column=table_col).value)
new_text = str(sheet.cell(row=table_row, column=table_col).value)
if ' ' in new_text:
new_text = new_text.split()[0]
# 文档Document - 段落Paragraph - 文字块Ru