如何把word檔案自動化

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

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

import office

from docx import Document

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

doc=Document("template.docx")

table=doc.tables0

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")

相关推荐
啊啊啊啊啊!!!!42 分钟前
【c++】stack和queue的接口使用以及底层实现
开发语言·c++
oyguyteggytrrwwwrt1 小时前
3dgs渲染部分详细注释
python
松仔log2 小时前
Java中级——组合和继承
android·java·开发语言
天天爱吃肉82183 小时前
【重磅发布:拿下新超仁达代理权 】
大数据·人工智能·python·功能测试·汽车
(Charon)3 小时前
【C++】锁与原子操作(四):自旋锁的完整实现与性能优化
c语言·开发语言·c++
神王宝宝 王者小学3 小时前
面向领域驱动架构的查询实现方式
前端·python·架构
雨田言炎4 小时前
五、Qt控件使用说明大全
开发语言·qt
bu_shuo5 小时前
MATLAB中的meshgrid和ndgrid
开发语言·matlab
ningmengjing_5 小时前
Redis 从入门到实战:Python操作全攻略
数据库·redis·python
️学习的小王5 小时前
智能文档助手:基于RAG的本地化文档问答系统实战指南
人工智能·python·机器学习