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

相关推荐
cxr8288 分钟前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱
MC皮蛋侠客20 分钟前
SQLAlchemy 系列(七):高级建模与高效写入——批量 DML、方言与扩展
数据库·python
Zane19941 小时前
@property 到底是怎么把方法伪装成属性的?一文吃透 property、staticmethod、classmethod
后端·python
qq_316411031 小时前
AI 情感陪伴智能潮玩软硬件一体化开发案例
人工智能·python
废弃的小码农1 小时前
功能测试--Day07--Python编程基础
开发语言·python
zx1154501 小时前
大模型工具调用次数限制
人工智能·python
fengci.2 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
MC皮蛋侠客2 小时前
SQLAlchemy 系列(八):AsyncIO、并发与 Web 生命周期——让每个并发任务持有自己的 Session
数据库·python
程序员zgh2 小时前
C++ 拷贝赋值运算符 详解
c语言·开发语言·c++
念何架构之路2 小时前
restartmanager-重启管理子系统
java·开发语言