如何把word檔案自動化

1.安裝 pip install jojo-office ( 安裝這個)

2.讀取表格內容與文章內容,範例如下

import office

from docx import Document

replace={"{user_name}":"陳彥甫", "{tele}":"0912882881"}

doc=Document("template.docx")

table=doc.tables[0]

print("ii:"+ str(len(table.rows)))

print("jj:"+ str(len(table.columns)))

for old,new in replace.items():

print(r,w)

for table in doc.tables:

for col in table.columns:

for cell in col.cells:

if cell.text == old:

cell.text = new

print(cell.text)

复制代码
for paragraph in doc.paragraphs:
    # 整串找到有字串一樣的地方,就直接更換
    if paragraph.text.find(old) >= 0:        
        paragraph.text = paragraph.text.replace(old,new)
        print("OK")  #找到,整串換掉

doc.save("test.docx")

相关推荐
m0_5312371716 分钟前
C语言-初始化赋值,函数,变量的作用域与生命周期
c语言·开发语言
张3蜂21 分钟前
Python venv 详解:为什么要用、怎么用、怎么用好
开发语言·python
zyeyeye25 分钟前
自定义类型:结构体
c语言·开发语言·数据结构·c++·算法
老赵全栈实战30 分钟前
《从零搭建RAG系统第3天:文档加载+文本向量化+向量存入Milvus》
python
火龙果研究院33 分钟前
在CentOS上安装Python 3.13需要从源码编译
开发语言·python·centos
invicinble1 小时前
关于学习技术栈的思考
java·开发语言·学习
龙山云仓1 小时前
No156:AI中国故事-对话司马迁——史家绝唱与AI记忆:时间叙事与因果之链
大数据·开发语言·人工智能·python·机器学习
niuniudengdeng1 小时前
一种基于高维物理张量与XRF实景复刻的一步闭式解工业级3D打印品生成模型
人工智能·python·数学·算法·3d
航哥的女人1 小时前
C++文件操作
开发语言·c++
overmind1 小时前
oeasy Python 114 在列表指定位置插入insert
开发语言·python