Python實(shí)現(xiàn)修改PDF文件內(nèi)部屬性值
部分 PDF 生成時(shí)會(huì)自動(dòng)嵌入一些隱藏屬性,比如創(chuàng)建軟件版本、電腦用戶名、修改記錄等,這些信息可能涉及隱私或商業(yè)機(jī)密。例如,用個(gè)人電腦編輯的公司文件,屬性中若包含個(gè)人用戶名,可能泄露信息歸屬;通過(guò)修改或清除這些屬性,可以避免不必要的信息暴露,降低隱私泄露風(fēng)險(xiǎn)。
pdf文件的屬性值怎么修改?要修改PDF文件的屬性值(如標(biāo)題、作者、主題等元數(shù)據(jù)),可以使用不同的工具或編程語(yǔ)言。以下是幾種常見(jiàn)的方法:
方法一:使用PDF編輯軟件(如Adobe Acrobat)
1. 打開(kāi)Adobe Acrobat,導(dǎo)入需要修改屬性的PDF文件。
2. 依次點(diǎn)擊“文件”→“屬性”(或按Ctrl+D)。
3. 在彈出的“文檔屬性”對(duì)話框中,切換到“描述”選項(xiàng)卡。
4. 修改所需的屬性值,如標(biāo)題、作者、主題、關(guān)鍵詞等。
5. 點(diǎn)擊“確定”保存更改。
方法二:使用Python(PyPDF2庫(kù))
如果你想通過(guò)編程方式修改PDF屬性,可以使用Python的PyPDF2庫(kù)。以下是示例代碼:
import PyPDF2 def modify_pdf_metadata(input_path, output_path, metadata): # 讀取原始PDF文件 with open(input_path, 'rb') as file: reader = PyPDF2.PdfReader(file) writer = PyPDF2.PdfWriter() # 復(fù)制所有頁(yè)面到寫(xiě)入器 for page in reader.pages: writer.add_page(page) # 獲取原始元數(shù)據(jù)并更新 original_metadata = reader.metadata writer.add_metadata({ '/Title': metadata.get('title', ''), '/Author': metadata.get('author', ''), '/Subject': metadata.get('subject', ''), '/Keywords': metadata.get('keywords', ''), # 可以添加更多元數(shù)據(jù)字段 }) # 寫(xiě)入新的PDF文件 with open(output_path, 'wb') as output_file: writer.write(output_file) # 使用示例 metadata = { 'title': '新標(biāo)題', 'author': '新作者', 'subject': '新主題', 'keywords': '關(guān)鍵詞1,關(guān)鍵詞2' } modify_pdf_metadata('input.pdf', 'output.pdf', metadata)
方法三:使用命令行工具(如pdftk或exiftool)
如果你偏好命令行操作,可以使用工具如`pdftk`或`exiftool`:
# 使用pdftk(需要先安裝) pdftk input.pdf dump_data output metadata.txt # 導(dǎo)出元數(shù)據(jù)到文本文件 # 編輯metadata.txt文件中的屬性值 pdftk input.pdf update_info metadata.txt output output.pdf # 更新元數(shù)據(jù) # 使用exiftool(需要先安裝) exiftool -Title="新標(biāo)題" -Author="新作者" -Subject="新主題" input.pdf -o output.pdf
根據(jù)你的具體需求和場(chǎng)景,選擇最適合的方法即可。如果需要批量處理多個(gè)PDF文件,編程方式會(huì)更加高效。
方法四:匯幫元數(shù)據(jù)編輯器
如果覺(jué)得上面的修改方法比較繁瑣,可以試試專門的元數(shù)據(jù)修改工具進(jìn)行修改比如“匯幫元數(shù)據(jù)編輯器”,適合小白操作,功能豐富。
需要注意的是并非所有屬性都能隨意改動(dòng):?
1、基礎(chǔ)屬性(標(biāo)題、作者、關(guān)鍵詞等)通??芍苯泳庉嫞?
2、加密文件的屬性可能被鎖定,需先解密(獲取權(quán)限密碼)才能修改;?
3、帶有數(shù)字簽名的 PDF,修改屬性可能導(dǎo)致簽名失效,需先確認(rèn)是否允許修改(部分簽名文件僅支持查看屬性)。
方法補(bǔ)充
python 修改文件屬性
1、獲取文件的創(chuàng)建、修改、訪問(wèn)時(shí)間
# -*- encoding=utf-8 -*- import os import time def get_file_time(filename): filename = os.path.abspath(filename) create_time = os.path.getctime(filename) # 創(chuàng)建時(shí)間 print('old create time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(create_time)))) update_time = os.path.getmtime(filename) # 修改時(shí)間 print('old update time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(update_time)))) access_time = os.path.getatime(filename) # 訪問(wèn)時(shí)間 print('old access time:{}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(access_time)))) return create_time, update_time, access_time if __name__ == '__main__': get_file_time('E:/a.txt')
2、更改文件的修改、訪問(wèn)時(shí)間(創(chuàng)建時(shí)間沒(méi)查到怎么修改,暫時(shí)不記錄)
# -*- encoding=utf-8 -*- import os import time def set_file_time(filename, updatetime, access_time): # 先傳修改時(shí)間,再傳訪問(wèn)時(shí)間 filename = os.path.abspath(filename) new_updatetime = time.mktime(time.strptime(updatetime, '%Y-%m-%d %H:%M:%S')) new_access_time = time.mktime(time.strptime(access_time, '%Y-%m-%d %H:%M:%S')) os.utime(filename, (new_access_time, new_updatetime)) if __name__ == '__main__': set_file_time('E:/a.txt', '2018-01-08 10:50:20', '2019-07-15 04:03:01')
3、放在同一個(gè)py方便直接復(fù)制使用
# -*- encoding=utf-8 -*- import os import time def get_file_time(filename): filename = os.path.abspath(filename) # 創(chuàng)建時(shí)間 create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(filename))) # 修改時(shí)間 update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(filename))) # 訪問(wèn)時(shí)間 access_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getatime(filename))) return create_time, update_time, access_time def set_file_time(filename, updatetime, access_time): # 先傳修改時(shí)間,再傳訪問(wèn)時(shí)間 filename = os.path.abspath(filename) new_update_time = time.mktime(time.strptime(updatetime, '%Y-%m-%d %H:%M:%S')) new_access_time = time.mktime(time.strptime(access_time, '%Y-%m-%d %H:%M:%S')) os.utime(filename, (new_access_time, new_update_time)) def debug(): create_time, update_time, access_time = get_file_time('E:/a.txt') set_file_time('E:/a.txt', update_time, access_time) get_file_time('E:/a.txt') if __name__ == '__main__': debug()
4、補(bǔ)充修改文件的創(chuàng)建時(shí)間
import os import time from pywintypes import Time # 可以忽視這個(gè) Time 報(bào)錯(cuò)(運(yùn)行程序還是沒(méi)問(wèn)題的) from win32con import FILE_FLAG_BACKUP_SEMANTICS from win32con import FILE_SHARE_WRITE from win32file import CloseHandle from win32file import CreateFile from win32file import GENERIC_WRITE from win32file import OPEN_EXISTING from win32file import SetFileTime def modify_file_create_time(filename, create_time_str, update_time_str, access_time_str): try: format_str = "%Y-%m-%d %H:%M:%S" # 時(shí)間格式 # f = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) f = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) create_time = Time(time.mktime(time.strptime(create_time_str, format_str))) update_time = Time(time.mktime(time.strptime(update_time_str, format_str))) access_time = Time(time.mktime(time.strptime(access_time_str, format_str))) SetFileTime(f, create_time, update_time, access_time) CloseHandle(f) print('update file time success:{}/{}/{}'.format(create_time_str, update_time_str, access_time_str)) except Exception as e: print('update file time fail:{}'.format(e)) if __name__ == '__main__': cTime = "2019-12-13 21:51:02" # 創(chuàng)建時(shí)間 mTime = "2019-02-02 00:01:03" # 修改時(shí)間 aTime = "2019-02-02 00:01:04" # 訪問(wèn)時(shí)間 fName = r"a.txt" # 可以是文件也可以是文件夾 print(os.path.isdir(fName)) modify_file_create_time(fName, cTime, mTime, aTime)
到此這篇關(guān)于Python實(shí)現(xiàn)修改PDF文件內(nèi)部屬性值的文章就介紹到這了,更多相關(guān)Python修改PDF屬性內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV角點(diǎn)檢測(cè)的實(shí)現(xiàn)示例
角點(diǎn)通常被定義為兩條邊的交點(diǎn),本文主要介紹了OpenCV角點(diǎn)檢測(cè)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03基于python分布式爬蟲(chóng)并解決假死的問(wèn)題
這篇文章主要介紹了基于python分布式爬蟲(chóng)并解決假死的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多線程TCP服務(wù)器的教程
這篇文章主要介紹了用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多線程TCP服務(wù)器的教程,示例的運(yùn)行環(huán)境為Windows操作系統(tǒng),需要的朋友可以參考下2015-05-05在pycharm中使用pipenv創(chuàng)建虛擬環(huán)境和安裝django的詳細(xì)教程
這篇文章主要介紹了在pycharm中使用pipenv來(lái)創(chuàng)建虛擬環(huán)境和安裝django的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Python3.x+迅雷x 自動(dòng)下載高分電影的實(shí)現(xiàn)方法
這篇文章主要介紹了Python3.x+迅雷x 自動(dòng)下載高分電影的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01使用rpclib進(jìn)行Python網(wǎng)絡(luò)編程時(shí)的注釋問(wèn)題
這篇文章主要介紹了使用rpclib進(jìn)行Python網(wǎng)絡(luò)編程時(shí)的注釋問(wèn)題,作者講到了自己在編寫(xiě)服務(wù)器時(shí)要用unicode注釋等需要注意的地方,需要的朋友可以參考下2015-05-05Python搭建監(jiān)控平臺(tái)的實(shí)現(xiàn)示例
本文主要介紹了Python搭建監(jiān)控平臺(tái)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07