Skip to content

Export Google files as Office files in the filesystem - #472

Draft
shcheklein wants to merge 3 commits into
mainfrom
export-office-formats
Draft

shcheklein wants to merge 3 commits into
mainfrom
export-office-formats

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

The filesystem now exports Google files the way Drive's web app and rclone do (rclone's default --drive-export-formats docx,xlsx,pptx,svg).

Before and after

fs.get("root/reports", "local/", recursive=True)

root/reports holds a Doc, a Sheet, a Slides deck, a Drawing, a Form, a shortcut to the Doc, a shortcut to a folder and data.txt.

Before (main): the copy stops at doc-link with ApiRequestError: 400 The requested conversion is not supported. Main exports every item it can't download as text/plain, and Drive refuses that for shortcuts, Sheets, Drawings and Forms. By then the copy has written local/reports/data.txt and local/reports/doc, and doc holds the Doc's plain text with a BOM.

After:

local/reports/data.txt
local/reports/doc.docx
local/reports/doc-link.docx      # the shortcut's target, under the shortcut's name
local/reports/sheet.xlsx
local/reports/slides.pptx
local/reports/drawing.svg
WARNING Skipping root/reports/form: Drive has no download for root/reports/form (application/vnd.google-apps.form)
WARNING Skipping root/reports/folder-link: root/reports/folder-link is a shortcut to a folder

Other reads follow the same rule:

  • fs.cat_file("root/reports/doc") and fs.open(...) return the .docx bytes. Before, they returned the Doc's text.
  • A direct cat_file, open or get_file of a Form or a folder shortcut raises FileNotDownloadableError. Before, it raised a 400 ApiRequestError.
  • A shortcut whose target is deleted or not shared raises FileNotDownloadableError when you read it directly. A recursive get skips it with a warning.
  • The core API is unchanged: GetContentString(mimetype="text/plain") still returns a Doc's text.

Choices

  • Listing names stay Drive's names. ls, find and info show doc, not doc.docx. get adds the extension only when the local name is the Drive name, so fs.get(fs.find(p), "local/") and cat_file(path) work on every listed path. A different local name is used exactly as given: fs.get("root/reports/doc", "local/notes") writes local/notes. rclone shows doc.docx in listings and matches either name on lookup. Doing that here would change path resolution for every operation, so this PR doesn't. (Question 1)
  • Local name collisions keep the first item. A Doc report and a file report.docx both save as report.docx. One get() keeps the first one it saves and skips the other with a warning, like rclone's "Duplicate object found in source - ignoring".
  • Folder shortcuts are skipped, not followed. rclone follows them with only an ancestor-loop guard, so a shortcut to a sibling folder downloads that folder twice. (Question 2)
  • Shortcut targets come from the listing. The listing fields now include shortcutDetails(targetId,targetMimeType), so following a shortcut costs no extra request. Only a shortcut opened by its own ID needs one more files.get.
  • Opening by a bare file ID reads the item's type first. That is one request per filesystem, cached, so a Doc opened by ID also exports .docx. Main opened by ID with 1 request; this PR makes it 2 on the first open. get_file by ID already read this metadata on main. (Question 3)

Questions for review

  1. Should listings show doc.docx, as rclone does? That is a larger change, to name lookups across all operations.
  2. Should we follow folder shortcuts, as rclone does, with an ancestor-loop guard and duplicates allowed? Or keep skipping them?
  3. Is the extra metadata request on the first bare-ID open() OK? The alternative keeps 1 request and exports a Doc opened by ID as text, so not every read would follow the same rule.

Tests

The new live tests failed on main in a test-only run: run 47481052404. All 4 failed:

  • the recursive get stopped at the 400 conversion error;

  • cat_file of a Doc started with b'\xef\xbb' instead of PK;

  • cat_file of a Form and of a folder shortcut raised a 400 ApiRequestError.

  • Live: a recursive get of a folder with a Doc, Sheet, Slides, Drawing, Form, file shortcut, folder shortcut and plain file. cat_file of a Doc starts with PK. cat_file of a Form or a folder shortcut raises FileNotDownloadableError. The Google files are created blank, so each export stays far below Drive's 10 MB limit.

  • Offline:

    • local naming for get (directory destination, same name, own name);
    • a bare-ID open of a Doc exports .docx;
    • a dangling shortcut raises FileNotDownloadableError;
    • two items with one local name keep the first;
    • a skipped shortcut leaves its local name free for a later file.
  • Updated tests:

    • two field pins, for the added shortcutDetails field;
    • test_open_by_file_id and the two open block-size tests, for the metadata request;
    • test_newer_listing_determines_which_id_a_warm_read_uses. Its stale listing shows only a folder at the path, so that read now raises IsADirectoryError instead of downloading the folder's ID.

Follow-up

Shortcuts to link-shared files that need shortcutDetails.targetResourceKey aren't handled. The filesystem doesn't send resource keys for any listed item, on main either, so that is a separate change.

Overlap with other drafts

Docs: a new "Google files and shortcuts" section in docs/fsspec.rst, and a note in docs/upgrading.rst.

🤖 Generated with Claude Code

Reads of Docs, Sheets, Slides and Drawings now export .docx, .xlsx,
.pptx and .svg, as Drive's web app and rclone do. get() adds the
extension when it names the local file after the Drive item. A
recursive get() follows shortcuts to files and skips Forms, shortcuts
to folders and shortcuts whose target is gone, with a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved destination-name collisions can silently overwrite downloads, while nested fs.get() calls corrupt thread-local state and can raise AttributeError after successful transfers.

2 open issues
What changed in this PR

This PR makes filesystem reads export Google files as Office files or SVG, while leaving the core API unchanged.

Changes:

  • Adds format-aware downloads and local filename extensions.
  • Follows file shortcuts and skips unsupported items during recursive downloads.
  • Documents the behavior and adds live and offline tests.
File Description
pydrive2/​test/​test_fs.py Tests live exports and shortcut handling.
pydrive2/​test/​test_fs_unit.py Tests naming, bare-ID exports, and missing targets.
pydrive2/​test/​test_fs_registration.py Updates bare-ID request expectations.
pydrive2/​fs/​spec.py Implements export formats, shortcut resolution, and download naming.
docs/​upgrading.rst Explains the filesystem behavior change.
docs/​fsspec.rst Documents exports, shortcuts, and read semantics.

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
A Doc named "a" and a file named "a.docx" both save as a.docx; get()
now skips the second with a warning instead of overwriting the first.
A get() started from a callback inside another get() no longer clears
the outer call's state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved destination tracking, directory collisions, and missing shortcut resource keys can skip valid downloads or abort recursive copies.

3 open issues
2 resolved since last review

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
A shortcut whose target is gone no longer blocks a later file that
saves under the same local name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants