日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python参考文献_[zotero/python]库中参考文献条目删除后,清除残留PDF的脚本

發(fā)布時間:2025/3/12 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python参考文献_[zotero/python]库中参考文献条目删除后,清除残留PDF的脚本 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

更新:使用 @滏陽河邊捉蚯蚓 https://zhuanlan.zhihu.com/p/41297136上獲取系統(tǒng)PDF文件和zotero.sqlite文件的代碼,在此感謝!

在zotero的library中刪除參考文獻(xiàn)條目后,有時PDF不會同步刪除,尤甚是安裝了zotfile插件后,添加參考文獻(xiàn)條目后,對應(yīng)的PDF附件會移動后zotfile指定的目錄中,刪除條目后PDF也不會同步刪除,因此寫了個腳本。運(yùn)行后,如果目錄中某個PDF沒有對應(yīng)的參考文獻(xiàn)條目,這個PDF就會被轉(zhuǎn)移到備份的文件夾中。

其原理是讀取PDF目錄中PDF文件的文件名,如a.pdf,b.pdf....,再讀取zoter.sqlite文件中的附件目錄,如果后者不包括a.pdf,則移動PDF目錄中的a.pdf到備份的目錄中。

將代碼復(fù)制為.py文件,如zot.py,在命令提示符下運(yùn)行:

python zot.py

運(yùn)行時需要關(guān)閉zotero,自己指定選擇備份的目錄。

如果有多余的PDF,則提示已備份***.pdf到備份目錄中。

如果移動錯了,將備份文件目錄中的文件自己復(fù)制回去即可。如果確認(rèn)無誤,再自行刪除備份的文件。

# -*- coding: utf-8 -*- """ Spyder Editor""" from __future__ import print_function from os import walk, remove import os import re import tkinter as tk from tkinter import filedialog import shutil #移動或復(fù)制文件 import sqlite3 import pandas as pd import configparser import shutil import sysroot = tk.Tk() root.withdraw()#選擇PDF備份的目錄 back_dir = filedialog.askdirectory(title = '請選擇PDF備份的目錄:')try:from pathlib import Path except ImportError:from pathlib2 import Pathif sys.version_info.major == 2:reload(sys)sys.setdefaultencoding('UTF8')def get_zotfile_dest_and_zotero_data_dirs():'''Get the Zotero data dir and the Zotfile destination dir in PosixPath type'''profile_dirs = {'darwin': Path.home() / 'Library/Application Support/Zotero','linux': Path.home() / '.zotero/zotero','linux2': Path.home() / '.zotero/zotero','win32': Path.home() / 'AppData/Roaming/Zotero/Zotero'}profile_dir = profile_dirs[sys.platform]config = configparser.ConfigParser()config.read('{}'.format(profile_dir / 'profiles.ini'))configs_loc = profile_dir / config['Profile0']['Path'] / 'prefs.js'configs = configs_loc.read_text()zotero_data_pat = re.compile(r'user_pref("extensions.zotero.dataDir", "(?P<zotero_data>.+)");')zotero_data_dir = Path(zotero_data_pat.search(configs).group('zotero_data'))zotfile_dest_pat = re.compile(r'user_pref("extensions.zotfile.dest_dir", "(?P<zotfile_dest>.+)");')zotfile_dest_dir = Path(zotfile_dest_pat.search(configs).group('zotfile_dest'))return zotero_data_dir, zotfile_dest_dirif __name__ == '__main__':#得到zotero數(shù)據(jù)目錄和文件目錄zotero_data_dir, zotfile_dest_dir = get_zotfile_dest_and_zotero_data_dirs()list_of_files = [] #文件包含目錄files = [] #僅文件名for (dirpath, dirnames, filenames) in walk(zotfile_dest_dir):for file in filenames:if file.endswith('.pdf') == True:list_of_files.append(os.path.join(dirpath, file))files.append(file)#連接數(shù)據(jù)庫zot_sqlite = os.path.join(zotero_data_dir, 'zotero.sqlite')with sqlite3.connect(zot_sqlite) as con:item_att=pd.read_sql_query("SELECT * FROM itemAttachments", con=con)item_path = item_att['path']#生成備份文件目錄#當(dāng)文件不存在時,才創(chuàng)建該文件夾。if not os.path.exists(back_dir):os.mkdir(back_dir)for i in range(len(files)):#如zotero.sqlite的path中不包括文件if not (item_path.str.contains(re.escape(files[i])).any()): #os.remove(list_of_files[i]) #也可以刪除文件shutil.move(list_of_files[i], os.path.join(back_dir, files[i]))#移動文件到備份目錄print('已備份',files[i],'到',back_dir)

win 10,Deepin 15.11測試通過。

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的python参考文献_[zotero/python]库中参考文献条目删除后,清除残留PDF的脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。