How to Export Google Drive Files in Different Formats Using gogcli
Use the gog docs export, gog sheets export, or gog slides export sub-commands with the --format flag to convert Google Workspace files to PDF, DOCX, CSV, XLSX, PPTX, or TXT, while binary files use gog drive download without format conversion.
The gogcli tool by steipete provides a command-line interface for exporting Google Drive files in various formats. Whether you need to convert Google Docs to Microsoft Word format or download Sheets as CSV, understanding how to export Drive files in different formats using gogcli streamlines your document workflow.
Understanding the gogcli Export Architecture
The export system in gogcli relies on a layered architecture that separates service-specific command handling from generic Drive export logic.
The Core Export Function
At the heart of the system is the exportViaDrive function in internal/cmd/export_via_drive.go. This function handles validation, resolves target files, determines the correct MIME type for export, and manages the download stream. It accepts an exportViaDriveOptions struct that specifies the expected MIME type, file kind label, and default format for each service.
Service-Specific Wrappers
Each Google Workspace service implements a thin wrapper command that delegates to the core exporter:
DocsExportCmdininternal/cmd/docs.gocallsexportViaDrivewith document-specific optionsSheetsExportCmdininternal/cmd/sheets.gohandles spreadsheet exportsSlidesExportCmdininternal/cmd/slides.gomanages presentation exports
Export Google Docs to PDF, DOCX, and TXT
Google Docs support three export formats via the --format flag. The mapping logic resides in internal/cmd/drive.go within the driveExportMimeTypeForFormat function.
To export a Google Doc to PDF (default):
gog docs export <DOC_ID> --out ~/Downloads/mydoc.pdf
To export to Microsoft Word format:
gog docs export <DOC_ID> --format docx --out ~/Downloads/mydoc.docx
To export to plain text:
gog docs export <DOC_ID> --format txt --out ~/Downloads/mydoc.txt
The system validates that the file's MIME type matches application/vnd.google-apps.document before proceeding with the export.
Export Google Sheets to CSV, XLSX, and PDF
Spreadsheets support CSV, Excel, and PDF formats. The SheetsExportCmd in internal/cmd/sheets.go passes expectedMime: "application/vnd.google-apps.spreadsheet" to the exporter.
Export to CSV:
gog sheets export <SHEET_ID> --format csv --out ~/Downloads/data.csv
Export to Excel format:
gog sheets export <SHEET_ID> --format xlsx --out ~/Downloads/data.xlsx
Export to PDF:
gog sheets export <SHEET_ID> --format pdf --out ~/Downloads/data.pdf
Export Google Slides to PPTX and PDF
Presentations support PowerPoint and PDF exports via SlidesExportCmd in internal/cmd/slides.go.
Export to PowerPoint:
gog slides export <SLIDES_ID> --format pptx --out ~/Downloads/presentation.pptx
Export to PDF:
gog slides export <SLIDES_ID> --format pdf --out ~/Downloads/presentation.pdf
Export Google Drawings to PNG and PDF
For Google Drawings, use the generic drive download command with the --format flag:
Export to PNG (default):
gog drive download <DRAWING_ID> --format png --out ~/Downloads/drawing.png
Export to PDF:
gog drive download <DRAWING_ID> --format pdf --out ~/Downloads/drawing.pdf
The drive.go file contains the driveExportMimeTypeForFormat function that maps drawing formats to image/png or application/pdf MIME types.
Download Binary Files Without Format Conversion
For non-Workspace files (ZIP, images, PDFs uploaded as binaries), use gog drive download without the --format flag:
gog drive download <FILE_ID> --out ~/Downloads/archive.zip
The downloadDriveFile function in internal/cmd/drive.go detects that the file is not a Google Workspace type (lacks the application/vnd.google-apps. prefix) and uses the Files.Get endpoint with alt=media instead of Files.Export.
Summary
- Use service-specific commands (
gog docs export,gog sheets export,gog slides export) for Google Workspace files to ensure proper MIME type validation and format conversion. - Specify formats with the
--formatflag; supported values includepdf,docx,txt,csv,xlsx,pptx, andpng. - Use
gog drive downloadfor Google Drawings or binary files; omit--formatfor non-Workspace files to stream raw bytes. - Core implementation resides in
internal/cmd/export_via_drive.goandinternal/cmd/drive.go, with service wrappers ininternal/cmd/docs.go,sheets.go, andslides.go.
Frequently Asked Questions
What export formats does gogcli support for Google Docs?
gogcli supports exporting Google Docs to pdf (default), docx (Microsoft Word), and txt (plain text). The driveExportMimeTypeForFormat function in internal/cmd/drive.go maps these format strings to their respective MIME types: application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document, and text/plain.
Can I export Google Sheets to Excel format using gogcli?
Yes. Use the command gog sheets export <ID> --format xlsx --out file.xlsx. The exporter validates that the file is a Google Spreadsheet (application/vnd.google-apps.spreadsheet) and maps the xlsx format to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet before calling the Drive API export endpoint.
How does gogcli handle binary file downloads differently from Google Workspace exports?
For binary files (non-Google Workspace files), gogcli uses the Files.Get endpoint with alt=media to stream raw bytes directly. For Google Workspace files (Docs, Sheets, Slides, Drawings), it uses the Files.Export endpoint with a specific MIME type derived from the --format flag. This logic is implemented in the downloadDriveFile function within internal/cmd/drive.go.
Where is the core export logic implemented in the gogcli source code?
The core export logic resides in internal/cmd/export_via_drive.go, specifically the exportViaDrive function (lines 26-122). This function handles ID normalization, path expansion, file resolution, MIME type validation, and delegates to downloadDriveFile in internal/cmd/drive.go for the actual HTTP request. Service-specific wrappers like DocsExportCmd, SheetsExportCmd, and SlidesExportCmd live in their respective files (docs.go, sheets.go, slides.go) and configure the options passed to the core exporter.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →