安装包
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)))
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。