目標
最近實驗室里成立了一個計算機興趣小組
倡議大家多把自己解決問題的經驗記錄並分享
就像在CSDN寫博客一樣
雖然剛剛起步
但考慮到后面此類經驗記錄的資料會越來越多
所以一開始就要做好模板設計(如下所示)
方便后面建立電子數據庫
從而使得其他人可以迅速地搜索到相關記錄
據說“人生苦短,我用python”
所以決定用python從docx文檔中提取文件頭的信息
然后把信息更新到一個xls電子表格中,像下面這樣(直接po結果好了)
而且點擊文件路徑可以直接打開對應的文件(含超鏈接)
代碼實現
1. 采集docx里面文件頭信息
# -*- coding:utf-8 -*-
# 此程序可掃描Log中的docx文件並返回基本信息
import docx
from docx import Document
test_d = '../log/sublime搭建python的集成開發環境.docx'
def docxInfo(addr):
document = Document(addr)
info = {'title':[],
'keywords':[],
'author':[],
'date':[],
'question':[]}
lines = [0 for i in range(len(document.paragraphs))]
k = 0
for paragraph in document.paragraphs:
lines[k] = paragraph.text
k = k+1
index = [0 for i in range(5)]
k = 0
for line in lines:
if line.startswith('標題'):
index[0] = k
if line.startswith('關鍵詞'):
index[1] = k
if line.startswith('作者'):
index[2] = k
if line.startswith('日期'):
index[3] = k
if line.startswith('問題描述'):
index[4] = k
k = k+1
info['title'] = lines[index[0]+1]
keywords = []
for line in lines[index[1]+1:index[2]]:
keywords.append(line)
info['keywords'] = keywords
info['author'] = lines[index[2]+1]
info['date'] = lines[index[3]+1]
info['question'] = lines[index[4]+1]
return info
if __name__ == '__main__':
print(docxInfo(test_d))
2. 遍歷log文件夾,進行信息更新
# -*- coding:utf-8 -*-
# 此程序可以批量掃描log中的文件,如果碰到docx文檔,
# 則調用readfile()提取文檔信息,並將信息保存到digger
# 日志列表.xls之中,方便后期快速檢索
import os,datetime
import time
import xlrd
from xlrd import xldate_as_tuple
import xlwt
from readfile import docxInfo
from xlutils.copy import copy
# 打開日志列表讀取最近一條記錄的更新日期
memo_d = '../log/digger日志列表.xls'
memo = xlrd.open_workbook(memo_d) #讀取excel
sheet0 = memo.sheet_by_index(0) #讀取第1張表
memo_date = sheet0.col_values(5) #讀取第5列
memo_n = len(memo_date) #去掉標題
if memo_n>0:
xlsx_date = memo_date[memo_n-1] #讀取最后一條記錄的日期,
latest_date = sheet0.cell_value(memo_n-1,5)
# 返回時間戳
# 新建一個xlsx
memo_new = copy(memo)
sheet1 = memo_new.get_sheet(0)
# 重建超鏈接
hyperlinks = sheet0.col_values(6) # xlrd讀取的也是text,造成超鏈接丟失
k = 1
n_hyperlink = len(hyperlinks)
for k in range(n_hyperlink):
link = 'HYPERLINK("%s";"%s")' %(hyperlinks[k],hyperlinks[k])
sheet1.write(k,6,xlwt.Formula(link))
k = k+1
# 判斷文件后綴
def endWith(s,*endstring):
array = map(s.endswith,endstring)
if True in array:
return True
else:
return False
# 遍歷log文件夾並進行查詢
log_d = '../log'
logFiles = os.listdir(log_d)
for file in logFiles:
if endWith(file,'.docx'):
timestamp = os.path.getmtime(log_d+'/'+file)
if timestamp>latest_date:
info = docxInfo(log_d+'/'+file)
sheet1.write(memo_n,0,info['title'])
keywords_text = ','.join(info['keywords'])
sheet1.write(memo_n,1,keywords_text)
sheet1.write(memo_n,2,info['author'])
sheet1.write(memo_n,3,info['date'])
sheet1.write(memo_n,4,info['question'])
#獲取當前時間
time_now = time.time() #浮點值,精確到毫秒
sheet1.write(memo_n,5, time_now)
link = 'HYPERLINK("%s";"%s")' %(file,file)
sheet1.write(memo_n,6,xlwt.Formula(link))
memo_n = memo_n+1
os.remove(memo_d)
memo_new.save(memo_d)
print('memo was updated!')
其實還有一些操作電子表格更好的模塊,比如panda、xlsxwriter、openpyxl等。不過上述代碼已經基本能實現功能,而且科研狗畢竟沒那么多時間寫代碼做調試,所以后面有空再update吧!
————————————————
版權聲明:本文為CSDN博主「sheldonxxd」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/sheldonxxd/article/details/80544831