安装包

pip install  python_docx
pip install xlrd

代码示例

from docx import Document
import xlrd

def change_text(old_text,new_text):#
    #读取文章中所有段落列表
    all_paras= document.paragraphs
    for run in all_paras:
        run_text=run.text.replace(old_text,new_text)
        run.text=run_text
    all_tables=document.tables
    for tabel in all_tables:
        for row in tabel.row:
            for cell in row.cells:
                cell_text=cell_text.replace(old_text,new_text)
                cell.text=cell_text

xlsx=xlrd.open_workbook('C://Users//fuxingyu//Desktop//hetong//信息.xlsx')
sheet=xlsx.sheet_by_index(0)
for table_row in range(1,sheet.nrows):
    document=Document("C://Users//fuxingyu//Desktop//hetong//合同.docx")
    for tabel_col in range(0,sheet.ncols):
        change_text(str(sheet.cell_value(0,tabel_col)),str(sheet.cell_value(table_row,tabel_col)))
    document.save("C://Users//fuxingyu//Desktop//hetong//%s合同.docx"%str(sheet.cell_value(table_row,0)))
    print("%s合同完成"%str(sheet.cell_value(table_row,0)))

效果

image.pngimage.png


叫我瞄大人
467 声望81 粉丝

喜欢追星的非科班的编程爱好者


引用和评论

0 条评论