Python實(shí)現(xiàn)將Excel內(nèi)容插入到Word模版中
前言
前段時(shí)間因?yàn)樾枰幚硪淮蠖羊?yàn)收單,都是一些簡(jiǎn)單的復(fù)制粘貼替換工作,于是就想到用python進(jìn)行處理。接下來進(jìn)入正題~
實(shí)現(xiàn)需求
我是用的開發(fā)環(huán)境是
- python 3.6
- openpyxl 3.1.1
- docx 0.2.4
需求

這個(gè)是從公司平臺(tái)導(dǎo)出的訂單詳情excel文件

這個(gè)是公司驗(yàn)收單模版
我這邊需求是把Excel文件中的訂單號(hào)、下單公司、套餐、數(shù)量分別添加到模版的訂單編號(hào)、甲方、驗(yàn)收測(cè)試內(nèi)容中,簡(jiǎn)單來說就是通過python腳本,將excel文件的訂單號(hào)、下單公司、套餐、數(shù)量分別替換word文件中的OrderID、Company、Package、Quantity
實(shí)現(xiàn)代碼
明確需求后直接上代碼
import openpyxl
import docx
import datetime
def get_excel_data():
# 打開Excel文件
wb = openpyxl.load_workbook('下單明細(xì).xlsx')
ws = wb['Sheet1']
# 獲取序列號(hào)
for cell in ws['A']:
Number.append(cell.value)
# 獲取訂單號(hào)
for cell in ws['C']:
OrderID.append(cell.value)
# OrderID.pop(0)
# 獲取數(shù)量
for cell in ws['F']:
Quantity.append(cell.value)
# 獲取公司名稱
for cell in ws['B']:
Company.append(cell.value)
# 獲取訂單套餐
for cell in ws['D']:
Package.append(cell.value)
# 替換word文檔內(nèi)容
for i in range(len(Number)):
# 打開word文檔
new_doc = docx.Document('交付驗(yàn)收單.docx')
for p in new_doc.paragraphs:
for r in p.runs:
# print(r.text)
if 'OrderID' in r.text: # 替換訂單號(hào)
item = OrderID[i]
r.font.underline = True
r.text = r.text.replace('OrderID', item)
print('OrderID' + '更改為' + str(item))
if 'Quantity' in r.text: # 替換數(shù)量
item = Quantity[i]
r.font.underline = True
r.text = r.text.replace('Quantity', str(item))
print('Quantity' + '更改為' + str(item))
if 'Company' in r.text: # 替換公司名稱
item = Company[i]
r.font.underline = True
r.text = r.text.replace('Company', str(item))
print('Company' + '更改為' + str(item))
if 'Package' in r.text: # 替換訂單套餐
item = Package[i]
r.font.underline = True
r.text = r.text.replace('Package', str(item))
print('Package' + '更改為' + str(item))
# 替換日期 #這里因?yàn)榭梢灾苯痈哪0嫠凶⑨尩袅耍枰砷_啟
# if 'Yy' in p.text:
# p.text = p.text.replace('Yy', str(year))
# if 'Mm' in p.text:
# p.text = p.text.replace('Mm', str(month))
# if 'Dd' in p.text:
# p.text = p.text.replace('Dd', str(day))
# 保存新文檔 #文件命名格式:交付驗(yàn)收單-公司名稱時(shí)間序號(hào).docx
new_doc.save('交付驗(yàn)收單-'+ str(Company[i]) +str(year)+str(month)+str(day)+'-' + str(Number[i]) + '.docx')
if __name__ == "__main__":
Number = []
OrderID = []
Quantity = []
Company = []
Package = []
now = datetime.datetime.now()
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
get_excel_data()運(yùn)行效果
終端:

文件夾保存文件:

注意:這里我為了方便以及更直觀的看到效果,把Excel文件表頭欄也進(jìn)行替換了,后續(xù)如果需要可以使用
OrderID.pop(0)將表頭欄參數(shù)刪掉,再把for循環(huán)次數(shù)減一即可
for i in range(len(Number) - 1):替換后的word文件:

到此這篇關(guān)于Python實(shí)現(xiàn)將Excel內(nèi)容插入到Word模版中的文章就介紹到這了,更多相關(guān)Python Excel內(nèi)容插入到Word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python?ConfigParser庫(kù)的使用及遇到的坑
這篇文章主要介紹了python?ConfigParser庫(kù)的使用及遇到的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
解決python3 安裝完P(guān)ycurl在import pycurl時(shí)報(bào)錯(cuò)的問題
今天小編就為大家分享一篇解決python3 安裝完P(guān)ycurl在import pycurl時(shí)報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
基于Python的身份證驗(yàn)證識(shí)別和數(shù)據(jù)處理詳解
這篇文章主要介紹了基于Python的身份證驗(yàn)證識(shí)別和數(shù)據(jù)處理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Django中常用的查詢數(shù)據(jù)方法及查詢對(duì)象的條件詳解
在web 開發(fā)過程中,Django 與后臺(tái)數(shù)據(jù)庫(kù)的交互是必不可少的一項(xiàng),也是實(shí)現(xiàn)業(yè)務(wù)邏輯所需數(shù)據(jù)的重要方式,這篇文章主要給大家介紹了關(guān)于Django中常用的查詢數(shù)據(jù)方法及查詢對(duì)象條件的相關(guān)資料,需要的朋友可以參考下2021-09-09
使用pandas模塊實(shí)現(xiàn)數(shù)據(jù)的標(biāo)準(zhǔn)化操作
這篇文章主要介紹了使用pandas模塊實(shí)現(xiàn)數(shù)據(jù)的標(biāo)準(zhǔn)化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05

