如何把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 分钟前
LangGraph的介绍
java·开发语言
康小庄7 分钟前
Java阻塞队列——用法及常用场景
java·开发语言·数据库·spring boot·spring·jetty
yy.y--12 分钟前
Java多线程实例:输出线程名20次
java·开发语言
小鸡吃米…23 分钟前
TensorFlow 实现多层感知机学习
人工智能·python·tensorflow
WW、forever1 小时前
【服务器】上传服务器中数据至 FigShare(Python)
运维·服务器·python
2301_816997881 小时前
Word版本介绍与选择
c#·word·xhtml
消失的旧时光-19431 小时前
C++ 多线程与并发系统取向(七)—— 并发排障与工程纪律(从“会写”到“能控场”)
开发语言·c++·并发
宝贝儿好1 小时前
【强化学习】第十章:随机高斯策略
人工智能·python·深度学习·神经网络·机器人·自动驾驶
haosend1 小时前
【练习版】使用paramiko批量的查询,管理,配置路由器交换机
python·路由器·交换机·网络自动化