python生成各类合同

叫我瞄大人

安装包

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

阅读 1.2k

个人编程学习
python,matlab,VB

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

464 声望
76 粉丝
0 条评论

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

464 声望
76 粉丝
文章目录
宣传栏