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

相关推荐
zbguolei17 分钟前
第001篇 · 算盘与差分机
开发语言·php
ZHOUPUYU1 小时前
PHP 8.4 新特性实战指南:从属性钩子到异步编程
开发语言·php
SummerJokes1 小时前
Java 多线程专项实验|两种创建线程方式、synchronized 线程同步、银行存取款实战
java·开发语言·python
艾派森1 小时前
Python帕累托分析实战:用二八法则找出贡献80%营收的核心商品
android·开发语言·python
xyphf_和派孔明1 小时前
第九章: 上下文工程五大策略与六大模块交叉矩阵纵览
java·开发语言
纪念 2291 小时前
C++ 算法模板与错题 (差分)
开发语言·c++·算法
向科→向总工→空间动态计系统2 小时前
剃刀明盒:几何与时序双验证
大数据·开发语言·人工智能·python
杜 硕2 小时前
从零开始的C++生活——第十六讲
开发语言·数据结构·c++·生活
两点王爷2 小时前
StringBuffer 和 StringBuilder 高频面试题全解析
java·开发语言·面试