Python自動(dòng)化辦公——Excel寫(xiě)word表格
一、引言
最近到了畢業(yè)設(shè)計(jì)答辯的時(shí)候,老師讓我?guī)彤厴I(yè)生寫(xiě)一段畢業(yè)設(shè)計(jì)的功能就是提供一個(gè)學(xué)士學(xué)位授予申請(qǐng)表,根據(jù)定制化需求,編寫(xiě)定制化代碼。
二、數(shù)據(jù)準(zhǔn)備
docx格式的word如下圖。
再提供一個(gè)Excel表格,要求可以直接讀取表格里的對(duì)應(yīng)內(nèi)容,填入到word表格里的對(duì)應(yīng)位置。表格是我自己定義的如下表:
學(xué)生姓名 | 所在院系 | 班級(jí)簡(jiǎn)稱 | 學(xué)號(hào)(10位) | 學(xué)制 | 身份證號(hào) | 專業(yè)名稱 | 外語(yǔ)A級(jí) | 外語(yǔ)四級(jí) | 外語(yǔ)六級(jí) | 專業(yè)四級(jí) | 專業(yè)八級(jí) | 項(xiàng)目名稱1 | 項(xiàng)目名稱2 | 項(xiàng)目名稱3 | 項(xiàng)目名稱4 | 項(xiàng)目名稱5 | 項(xiàng)目名稱6 | 項(xiàng)目名稱7 | 項(xiàng)目名稱8 | 獎(jiǎng)勵(lì)績(jī)點(diǎn)1 | 獎(jiǎng)勵(lì)績(jī)點(diǎn)2 | 獎(jiǎng)勵(lì)績(jī)點(diǎn)3 | 獎(jiǎng)勵(lì)績(jī)點(diǎn)4 | 獎(jiǎng)勵(lì)績(jī)點(diǎn)5 | 獎(jiǎng)勵(lì)績(jī)點(diǎn)6 | 獎(jiǎng)勵(lì)績(jī)點(diǎn)7 | 獎(jiǎng)勵(lì)績(jī)點(diǎn)8 | 處分名稱1 | 處分名稱2 | 處分名稱3 | 罰扣績(jī)點(diǎn)1 | 罰扣績(jī)點(diǎn)2 | 罰扣績(jī)點(diǎn)3 | 處分時(shí)間1 | 處分時(shí)間2 | 處分時(shí)間3 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
天海 | 電子與信息工程學(xué)院 | 智能BG201 | 744411115555 | 4 | 52013145555 | 人工智能 | √ | √ | √ | 智慧農(nóng)業(yè)可視化 | 計(jì)算機(jī)視覺(jué)教輔 | 計(jì)賽國(guó)家一等獎(jiǎng) | 互聯(lián)網(wǎng)+省二等獎(jiǎng) | 0.2 | 0.2 | 0.4 | 0.1 | 偷吃老師抽屜的餅干 | 撒謊的人要吞一千根針哦 | 0.1 | 0.9 | 2023年6年15日 | 2022年12月14日 |
三、python代碼
那么根據(jù)定制化需求,我制作了三套代碼。
1、方法一
我們的邏輯是:
- 讀取Excel文件
- 找準(zhǔn)word中表格對(duì)應(yīng)位置與順序
- 插入讀取到Excel中的數(shù)據(jù)到word表格中
先展示最終嵌入到項(xiàng)目中的代碼吧.
首先需要引入docx的庫(kù)和pandas庫(kù),注意由于docx庫(kù)的版本不同使用的方法也有一定差異
import docx
from docx.enum.text import WD_ALIGN_PARAGRAPH
import pandas as pd
接下來(lái)進(jìn)行第一步,讀取表格數(shù)據(jù),并打開(kāi)word
#打開(kāi)或創(chuàng)建word文檔
doc_name = "test.docx"
doc = docx.Document(doc_name)
# 讀取Excel數(shù)據(jù)
df = pd.read_excel('source.xlsx', sheet_name='info')
content = [list(row) for _, row in df.iterrows()]
print(content)
人為分析了word的表格索引位置,并寫(xiě)入index中
table = doc.tables[0]
index = [[0,3],[0,12],[1,3],[1,12],[2,3],[2,12],[5,3],
[5,5],[5,10],[5,13],[5,16],[7,7],[7,16],[8,7],[8,16],
[9,7],[9,16],[10,7],[10,16],[12,3],[12,7],[12,11],
[13,3],[13,7],[13,11],[14,3],[14,7],[14,11]]
寫(xiě)入28條數(shù)據(jù)并將空的數(shù)據(jù)輸入空格,設(shè)置居中和宋體字體,隨后將輸出的word保存為學(xué)號(hào)+.docx
for i in range(0,28):
cell = table.cell(index[i][0],index[i][1])
cell.text = str(content[0][i])
if cell.text == 'nan':
cell.text = ' '
print('%d:'%i,cell.text)
cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
for par in cell.paragraphs:
for run in par.runs:
run.font.size = docx.shared.Pt(12)
run.font.name = '宋體'
filename = str(df.iloc[0, 3])
doc.save(filename + '.docx')
完整代碼如下:
file_name = "D:\\poppler-0.67.0_x86\\test1.docx"
doc = docx.Document(file_name)
df = pd.read_excel('D:\\poppler-0.67.0_x86\\學(xué)員表.xlsx', sheet_name='info')
print(df)
for item in range(df.shape[0]):
content = [list(row) for _, row in df.iterrows()]
table = doc.tables[0]
index = [[0, 3], [0, 12], [1, 3], [1, 12], [2, 3], [2, 12], [5, 3],
[5, 5], [5, 10], [5, 13], [5, 16], [7, 7], [7, 16], [8, 7], [8, 16],
[9, 7], [9, 16], [10, 7], [10, 16], [12, 3], [12, 7], [12, 11],
[13, 3], [13, 7], [13, 11], [14, 3], [14, 7], [14, 11]]
for i in range(0, 28):
cell = table.cell(index[i][0], index[i][1])
cell.text = str(content[item][i])
if cell.text == 'nan':
cell.text = ' '
cell.paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
for par in cell.paragraphs:
for run in par.runs:
run.font.size = docx.shared.Pt(12)
run.font.name = '宋體'
file_name = 'D:\\poppler-0.67.0_x86\\doc_output\\' + str(df.iloc[item, 3]) + '.docx'
# file_name = "D:\\poppler-0.67.0_x86\\7414119104.docx"
doc.save(file_name)
print("保存成功!")
這樣一來(lái),Excel里所有的行都會(huì)輸出為word文件,有幾行數(shù)據(jù)就能輸出幾個(gè)word,且以學(xué)號(hào)命名。
缺點(diǎn):需要自己分析模板word并寫(xiě)死索引
2、方法二
使用的方法二就是精簡(jiǎn)版的方法一,原理就是不考慮word表格分布寫(xiě)入數(shù)據(jù),而是自己在代碼里寫(xiě)一個(gè)新的word表格進(jìn)行插入數(shù)據(jù)。
具體步驟如下:
- 讀取Excel數(shù)據(jù)
- 生成、配置word的表格和名稱
- 填寫(xiě)表格
- 保存word
代碼如下:
這里我就是定義了兩個(gè)循環(huán)來(lái)生成簡(jiǎn)單的37行2列的表格并填寫(xiě)數(shù)據(jù)
import pandas as pd
from docx import Document
from docx.shared import Inches
# 讀取Excel數(shù)據(jù)
df = pd.read_excel('source.xlsx', sheet_name='info')
# 獲取表格標(biāo)題和內(nèi)容
header = list(df.columns)
content = [list(row) for _, row in df.iterrows()]
# 生成Word文件名
filename = df.iloc[0, 3]
# 配置Word
document = Document('output.docx')
table = document.add_table(rows=37, cols=2, style='Table Grid')
# 填寫(xiě)表格
for i, text in enumerate(header):
table.cell(i, 0).text = text
for i, row in enumerate(content):
for j, text in enumerate(row):
table.cell(j, i+1).text = str(text)
# 保存Word文件
document.save(f'{filename}.docx')
3、方法三
使用字符串匹配來(lái)進(jìn)行變量的寫(xiě)入,缺點(diǎn):無(wú)法進(jìn)行表格寫(xiě)入,可以在無(wú)表格的word文章中發(fā)揮很大優(yōu)勢(shì)。
可以使用pyqt5來(lái)制作一個(gè)小工具進(jìn)行文字替換。這里我也實(shí)現(xiàn)了,但由于代碼量過(guò)大,這里不做介紹啦,有需要的可以私信我,再進(jìn)行更新。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-498415.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-498415.html
到了這里,關(guān)于python自動(dòng)化辦公——定制化讀取Excel數(shù)據(jù)并寫(xiě)入到word表格的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!