# GoogleBookmarksToExcel ### 說明 * 這是將 Chrome 匯出的書籤轉換為 Excel,第一欄為 `title`,第二欄為 `url` * 匯出的檔案會儲存在桌面上加上時間戳 ### 安裝環境 #### 1\. 安裝需要的庫 在開始之前,請安裝 `pandas` 庫。打開命令提示字元或終端,並執行以下命令: ``` pip install pandas ``` #### 2\. 儲存程式碼 將以下程式碼複製並儲存為 `extract_bookmarks.py` 檔案。 #### 3\. 執行程式 在命令提示字元或終端中,執行程式: ``` python extract_bookmarks.py ``` 程式會詢問你輸入 HTML 檔案的路徑。例如: ``` 檔案路徑:C:\path\to\your\bookmarks.html ``` #### 4\. 查看結果 程式會將書籤資料匯出為 Excel 檔案,並儲存在桌面上。檔案名稱會包含原始檔案名稱和時間戳。 例如,匯出的檔案會顯示在桌面上,名稱如下: ``` bookmarks_20250121_153045.xlsx ``` #### 小提示 - 確保 HTML 檔案包含有效的 `<a>` 標籤,程式會從中提取書籤標題和 URL。 - 程式僅支援 `.html` 檔案格式。 ### 程式碼 ```python import os import re import pandas as pd from datetime import datetime # 使用正規表達式從 HTML 中擷取書籤標題與 URL def extract_bookmarks(file_path): bookmarks = [] with open(file_path, 'r', encoding='utf-8') as file: content = file.read() # 正規表達式匹配 <a> 標籤的 href 屬性和標題 pattern = r'<A HREF="(.*?)".*?>(.*?)</A>' matches = re.findall(pattern, content) for match in matches: url, title = match bookmarks.append({'title': title, 'url': url}) return bookmarks def process_html(file_path): if file_path.endswith('.html'): # 從 HTML 檔案擷取書籤資料 bookmarks = extract_bookmarks(file_path) # 取得檔案名稱並加上時間戳 base_filename = os.path.splitext(os.path.basename(file_path))[0] timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") output_filename = f"{base_filename}_{timestamp}.xlsx" # 匯出到 Excel df = pd.DataFrame(bookmarks) desktop_path = os.path.join(os.path.expanduser("~"), "Desktop", output_filename) # 桌面路徑 # 讓活頁表名稱與檔案名稱相同 with pd.ExcelWriter(desktop_path) as writer: df.to_excel(writer, index=False, sheet_name=base_filename) print(f"書籤已成功匯出至:{desktop_path}") else: print("請提供有效的書籤 HTML 檔案。") if __name__ == "__main__": # 詢問並處理 HTML 檔案路徑 file_path = input("請輸入 HTML 檔案路徑:").strip('"') # 去除兩端雙引號 process_html(file_path) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up