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

相关推荐
JIngJaneIL2 分钟前
基于Java非遗传承文化管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
吃西瓜的年年27 分钟前
1. 初识C语言
c语言·开发语言
ULTRA??32 分钟前
初学protobuf,C++应用例子(AI辅助)
c++·python
CHANG_THE_WORLD1 小时前
Python 字符串全面解析
开发语言·python
不会c嘎嘎1 小时前
深入理解 C++ 异常机制:从原理到工程实践
开发语言·c++
永远都不秃头的程序员(互关)1 小时前
C语言 基本语法
c语言·开发语言
甄心爱学习2 小时前
CSP认证 备考(python)
数据结构·python·算法·动态规划
永远都不秃头的程序员(互关)2 小时前
Java核心技术精要:高效实践指南
java·开发语言·性能优化
databook2 小时前
数据会说谎?三大推断方法帮你“审问”数据真相
后端·python·数据分析
是Dream呀2 小时前
Python圣诞特辑:打造一棵会唱歌、会下雪的魔法圣诞树
开发语言·python·pygame