如何把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")

相关推荐
码云数智-园园1 分钟前
前端跨域全解析:核心原理、解决方案选型与实战指南
开发语言
qq_417695052 分钟前
内存对齐与缓存友好设计
开发语言·c++·算法
2301_816651222 分钟前
实时系统下的C++编程
开发语言·c++·算法
2401_831824963 分钟前
C++与Python混合编程实战
开发语言·c++·算法
飞Link6 分钟前
告别 ROS 的臃肿:用 ZeroMQ 构建极速具身智能分布式大脑(附 Python 实战)
开发语言·分布式·python
qq_211387479 分钟前
基于LangGraph多agent
开发语言·前端·javascript·agent·langgraph
嫂子的姐夫9 分钟前
039-DES:gov招标(解密)
爬虫·python·js逆向·逆向
※※冰馨※※9 分钟前
【QT】TortoiseGit配 SSH 克隆 Codeup
开发语言·c++·windows
一只爱学习的小鱼儿10 分钟前
使用QT编写粒子显示热力图效果
开发语言·qt
2301_8166512211 分钟前
C++中的策略模式高级应用
开发语言·c++·算法