您好,登錄后才能下訂單哦!
這篇文章主要講解了“Python怎么實(shí)現(xiàn)將Excel內(nèi)容插入到Word模版中”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Python怎么實(shí)現(xiàn)將Excel內(nèi)容插入到Word模版中”吧!
我是用的開(kāi)發(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)單來(lái)說(shuō)就是通過(guò)python腳本,將excel文件的訂單號(hào)、下單公司、套餐、數(shù)量分別替換word文件中的OrderID、Company、Package、Quantity
明確需求后直接上代碼
import openpyxl import docx import datetime def get_excel_data(): # 打開(kāi)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)): # 打開(kāi)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嫠凶⑨尩袅?,需要可開(kāi)啟 # 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()
終端:
文件夾保存文件:
注意:這里我為了方便以及更直觀的看到效果,把Excel文件表頭欄也進(jìn)行替換了,后續(xù)如果需要可以使用
OrderID.pop(0)將表頭欄參數(shù)刪掉,再把for循環(huán)次數(shù)減一即可
for i in range(len(Number) - 1):替換后的word文件:
感謝各位的閱讀,以上就是“Python怎么實(shí)現(xiàn)將Excel內(nèi)容插入到Word模版中”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Python怎么實(shí)現(xiàn)將Excel內(nèi)容插入到Word模版中這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。