時間:2020年3月22日 22:08,
距離 “ 武漢發布 ”官方發布《湖北省內外人員返漢、省內人員離漢政策來了》 30分鐘,
大晚上的,公司行政小姐姐騷擾我,,,
我能怎么? 實在不好拒絕,,,,,,
準備階段
- 需要1張excel表格,記錄了每個員工的必要資訊, openpyxl模塊可以讀取excel
- 需要1個《復工證明_模板》.docx, 可以用python-docx來讀寫word
- 復工模板是一個模板,里邊能動態替換的地方是:ToReplace1, ToReplace2
- 復工模板word里邊標題和公司落款可以自定義化,可自行DIY,
Python代碼
import os
import openpyxl
from docx import Document
# 這個函式相對比較復雜,盡量不要修改,
def docx_replace(doc, key_word, new_word): #keyword是需要查找的關鍵字,new_word是替換后的新的文字
paragraphs = list(doc.paragraphs)
for p in paragraphs:
if key_word in p.text:
inline = p.runs # 切成片段,但是這個片段實在是太短了,
started = False
key_index = 0
found_runs = list() # 記錄在哪些index上匹配上了關鍵字
found_all = False
replace_done = False
for i in range(len(inline)):
# 直接在某個片段里找到了,則直接替換,
if key_word in inline[i].text and not started:
found_runs.append((i, inline[i].text.find(key_word), len(key_word)))
text = inline[i].text.replace(key_word, str(new_word))
inline[i].text = text # 替換
replace_done = True
found_all = True
break
# 未找到該片段,則繼續找
if key_word[key_index] not in inline[i].text and not started:
continue
# 在第一個片段里匹配到了部分,
if key_word[key_index] in inline[i].text and inline[i].text[-1] in key_word and not started:
start_index = inline[i].text.find(key_word[key_index])
check_length = len(inline[i].text)
for text_index in range(start_index, check_length):
if inline[i].text[text_index] != key_word[key_index]:
break
if key_index == 0:
started = True
chars_found = check_length - start_index
key_index += chars_found
found_runs.append((i, start_index, chars_found))
if key_index != len(key_word):
continue
else:
found_all = True
break
# 在其他的片段里找到了
if key_word[key_index] in inline[i].text and started and not found_all:
chars_found = 0
check_length = len(inline[i].text)
for text_index in range(0, check_length):
if inline[i].text[text_index] == key_word[key_index]:
key_index += 1
chars_found += 1
else:
break
found_runs.append((i, 0, chars_found))
if key_index == len(key_word):
found_all = True
break
if found_all and not replace_done:
for i, item in enumerate(found_runs):
index, start, length = [t for t in item]
if i == 0:
text = inline[index].text.replace(inline[index].text[start:start + length], str(new_word))
inline[index].text = text # 替換
else:
text = inline[index].text.replace(inline[index].text[start:start + length], '')
inline[index].text = text # 替換
# 以下是可以修改的,比如增加Excel的列,或者增加word里邊的“ReplaceX”
wb = openpyxl.load_workbook('員工名單資訊.xlsx') # 讀取excel里邊的內容
table = wb.active
rows = table.max_row
for r in range(2, rows + 1): #跟excel的第一行標題行無關,從第二行文字內容開始做替換作業
value1 = table.cell(row=r, column=1).value #獲取文字
value2 = table.cell(row=r, column=2).value
document = Document('復工證明_模板.docx') # 打開檔案demo.docx
docx_replace(document, "ToReplace1", value1) # 替換1
docx_replace(document, "ToReplace2", value2) # 替換2
document.save('復工證明_%s.docx' % value1) # 保存在當前目錄下面
print('復工證明_%s.docx自動生成完畢!' % value1)
os.system("pause")
程式打包成.exe
考慮到行政小姐姐,肯定不會用Python,也不具備Python運行環境,
幫她打包成.exe吧,,, 用pyinstaller 打包如下:
有點變態的大,沒辦法啊,py2exe打包出了點問題,不然不會用pyinstaller打包...
下載地址:
跳轉至自拍教程官網下載
運行方式與效果:
可編輯《員工名單資訊.xlsx》里的姓名,身份證號碼
可編輯《復工證明_模板.docx》里的標題,正文,還有公司落款,但是要保留ToReplace1, Replace2, 這兩處是自動替換的地方,
雙擊運行“docx_generator.exe"即可,
最終生成的返工(返崗)證明效果如下:
更多更好的原創文章,請訪問官方網站:www.zipython.com
自拍教程(自動化測驗Python教程,武散人編著)
原文鏈接:https://www.zipython.com/#/detail?id=ac62b9061e5f4d4582839f753da71b82
也可關注“武散人”微信訂閱號,隨時接受文章推送,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/167466.html
標籤:Python
上一篇:爬蟲之selenium
