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

相关推荐
leo__52034 分钟前
基于导航数据的扩展卡尔曼滤波(EKF)MATLAB 实现
开发语言·matlab
gugucoding1 小时前
25. 【C语言】二进制文件与随机读写
c语言·开发语言
北极星日淘1 小时前
中古货品品相评级算法实战|Java权重计分实现标准化五级品相体系
开发语言·python
hangyuekejiGEO1 小时前
临沂GEO服务企业技术选型分析
人工智能·python
小巧的砖头1 小时前
C#会重蹈覆辙吗?系列之2:反射及元数据的性能问题
开发语言·前端·c#
凯瑟琳.奥古斯特1 小时前
力扣1009补码解法C++实现
开发语言·c++·算法·leetcode·职场和发展
2601_963645921 小时前
【2026最新】MATLAB教程(附安装包,非常详细)
开发语言·matlab·信息可视化
闲猫3 小时前
Python 虚拟环境 virtualenv & uvicorn 服务搭建 & FAstAPI 使用
开发语言·python
AI视觉网奇3 小时前
vllm 多卡部署
python
精明的身影3 小时前
网络计划WebApp求解:融合Python与AI决策的项目管理系统
网络·python·web app