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

相关推荐
爱喝水的鱼丶2 分钟前
SAP-ABAP:SELECT大数据量查询性能调优——避免嵌套循环、减少数据库交互的核心方案
开发语言·数据库·sql·性能优化·sap·abap·erp
Dxy12393102165 分钟前
Python 实现POST上行压缩上传可用HTTP库汇总
开发语言·python·http
荣码10 分钟前
AI应用部署上线:Docker打包+API服务+监控告警,我踩了4个坑
java·python
郝学胜-神的一滴15 分钟前
Python 高级编程 026:序列内核深剖
开发语言·python·程序人生·软件工程
神奇霸王龙16 分钟前
Qwen3.7-Max屠榜:推理成本仅GPT-5.5的1/25
人工智能·python·gpt·ai·aigc·ai编程
xiaotianyuanma17 分钟前
【计算机毕业设计】基于java web的社区养老服务管理系统设计与实现
java·开发语言·课程设计
渣男教父21 分钟前
Python-openpyxl操作Excel
python
Wang's Blog25 分钟前
Go-Zero项目开发39: 熔断、限流与降级的高可用实践
开发语言·后端·golang·go-zero
Alan_69131 分钟前
第三方API对接的通用封装模式
服务器·开发语言·php