A tool to download private PDF files from OneDrive using Selenium.
Project description
📄 PDF Exporter from Authenticated OneDrive Sessions
This project allows you to export PDFs, even those that are protected, from authenticated OneDrive sessions using Selenium. The tool automates the browser process to capture screenshots of each page and combine them into a PDF file. Works also on OneDrive for Business.
[!WARNING] This tool may need to be calibrated in order to work correctly. It is expected to be used by someone who can inspect a page and read HTML.
✨ Features
- Supports both Firefox and Chrome browsers.
- Ability to export protected PDFs from authenticated web sessions.
- Can optionally keep or delete temporary images used for PDF creation.
- Compatible with browser profiles to retain session data (useful for skipping the login).
- Support for Raw lossless PDFs
👀 Preview
$ python OnedrivePrivatePDFDownloader.py --profile-dir /path/to/profile https://blabla.sharepoint.com/...
INFO - Initializing browser: firefox
Make sure to authenticate and reach the PDF preview.
INFO - Total number of pages detected: 8
INFO - Detected file name: '2024-10-21.pdf'
INFO - Starting the export of the file "2024-10-21.pdf". This might take a while depending on the number of pages.
INFO - Toolbar hidden for clean screenshots.
INFO - Page 1 of 8 exported.
INFO - Page 2 of 8 exported.
INFO - Page 3 of 8 exported.
INFO - Page 4 of 8 exported.
INFO - Page 5 of 8 exported.
INFO - Page 6 of 8 exported.
INFO - Page 7 of 8 exported.
INFO - Page 8 of 8 exported.
INFO - Saving the file as '2024-10-21.pdf'.
INFO - Temporary images removed.
INFO - Browser session ended.
📋 Requirements
Before running the project, you need the following dependencies:
🌐 Browsers:
Make sure that you have one of the following browsers installed:
- Firefox
- Chrome
🔧 Browser Drivers:
To interact with your browser via Selenium, you need the appropriate driver for your browser:
- Geckodriver for Firefox: Download Geckodriver
- Chromedriver for Chrome: Download Chromedriver
Ensure the drivers are in your system’s PATH or specify their location explicitly when launching the browser.
⚙️ Installation and Setup
-
Install via
pip:pip install onedrive_pdf_downloader
-
Download and install the appropriate browser drivers for your browser.
-
Optionally, set up a browser profile to retain session information:
- Firefox:
- Create a Firefox profile through
about:profilesin the browser's address bar. - Use the
-poption to specify the path to the Firefox profile directory.
- Create a Firefox profile through
- Chrome:
- Specify the Chrome user data directory and profile name using
-pand-noptions. You can find this inchrome://version/.
- Specify the Chrome user data directory and profile name using
- Firefox:
🚀 Usage
To run the script, use the following command structure:
onedrive-pdf-downloader [options]
⚡ Command-line Options
| Argument | Description | Example |
|---|---|---|
--browser, -b |
Specify the browser to use (firefox or chrome). |
--browser firefox |
--profile-dir, -p |
Path to the browser profile directory. If using Chrome, specify the user data directory. | --profile-dir /path/to/profile |
--profile-name, -n |
Profile name to use (Chrome only). | --profile-name "Profile 1" |
--keep-imgs, -k |
Keep the temporary images used for PDF creation. | --keep-imgs |
--output-file, -o |
Specify the output file name. | --output-file file.pdf |
--cache-dir, -r |
Search in browser caches for RAW Lossless PDFs (Firefox only). | --cache-dir /path/to/cache |
url |
The URL of the PDF file. This is a required argument. | https://blabla.sharepoint.com/... |
🗑️ Uninstall
To uninstall the package, you can use pip:
pip uninstall onedrive_pdf_downloader
🛠️ Profile Setup:
To use an authenticated session, you may need to use a browser profile where you're already logged in. Here’s how to do that:
-
Firefox Profile:
- Open Firefox and type
about:profilesin the address bar. - Create a new profile or use an existing one. The profile path will be displayed under "Root Directory."
- Use the
--profile-diroption to point to this directory in the script.
- Open Firefox and type
-
Chrome Profile:
- Open Chrome and type
chrome://versionin the address bar. - Find the
Profile Path - Use the
--profile-diroption for the user data directory (e.g.,/path/to/profiles) and the--profile-nameoption for the profile name (e.g.,Default).
- Open Chrome and type
📂 Getting the Cache Path:
To use the --cache-dir option, you need to find the cache directory of your browser. Here’s how to do that:
-
Firefox:
- Open Firefox and type
about:profilesin the address bar. - Locate the profile you are using and find the "Local Directory" path.
- The cache directory is typically located within this path under
cache2/entries.
Basically,
{profile_path}/cache2/entries. - Open Firefox and type
🛠️ Calibrating the Tool
If the tool is not working correctly, you may need to update the class names and ARIA labels used to identify elements on the OneDrive page. These values are defined in the OnedrivePrivatePDFDownloader.py file.
📝 Steps to Calibrate:
-
Open the OneDrive page in your browser:
- Use the browser's inspector tool (F12, Ctrl+Shift+I in most browsers, or right-click and select "Inspect") to find the class names or the ARIA labels for the elements used by the script.
-
Update the class names and ARIA labels in the script:
- Open the OnedrivePrivatePDFDownloader.py file.
- Update the following lists with the new values:
CLASS_NAMES_TOTAL_PAGES = ["status_5a88b9b2"] # Add the new class names for the total pages element CLASS_NAMES_FILE_NAME = ["OneUpNonInteractiveCommandNewDesign_156f96ef"] # Add the new class names for the file name element CLASS_NAMES_TOOLBAR = ["root_5a88b9b2"] # Add the new class names for the toolbar element ARIA_LABELS_NEXT_PAGE = ["Vai alla pagina successiva."] # Add the new ARIA labels for the next page button
-
Save the changes and run the script again:
- Save the updated OnedrivePrivatePDFDownloader.py file.
- Run the script with the updated values to ensure it works correctly.
By following these steps, you can calibrate the tool to work with any changes in the OneDrive page structure.
🤝 Contributing
We welcome contributions to improve this tool. If you have found new class names or ARIA labels, please consider submitting a pull request to update the configuration.
For more details, see the CONTRIBUTING.md file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file onedrive_pdf_downloader-1.0.1-py3-none-any.whl.
File metadata
- Download URL: onedrive_pdf_downloader-1.0.1-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6198cceaf55c1f458775695ca43a297177da1116dfa0dcf9454606bf9fa79c55
|
|
| MD5 |
92ffd5d2138e3e5b06c068e95fc09ed5
|
|
| BLAKE2b-256 |
bb19870dc5173b55e775f0a121a99d5ceedc40e742c14cb76f70f400d62c2e88
|