function getDocAsBlob(file, format) { const exportFormats = { 'docx': MimeType.MICROSOFT_WORD, 'pdf': MimeType.PDF, 'txt': MimeType.PLAIN_TEXT, 'html': MimeType.HTML, 'odt': MimeType.OPENDOCUMENT_TEXT, 'rtf': MimeType.RTF };
def download_document(self, doc_id, doc_name, format_type, save_path): export_mimes = { 'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'pdf': 'application/pdf', 'txt': 'text/plain', 'html': 'text/html', 'odt': 'application/vnd.oasis.opendocument.text', 'rtf': 'application/rtf' } request = self.service.files().export_media(fileId=doc_id, mimeType=export_mimes[format_type]) file_path = os.path.join(save_path, f"{doc_name}.{format_type}") with open(file_path, 'wb') as f: downloader = MediaIoBaseDownload(f, request) done = False while not done: status, done = downloader.next_chunk() print(f"Download {int(status.progress() * 100)}%.") download google docs desktop
def download_selected(self): selected = [] for item in self.tree.get_children(): if self.tree.item(item)['text'] == "☑": doc_id = self.tree.item(item)['tags'][0] doc_name = self.tree.item(item)['values'][0] selected.append((doc_id, doc_name)) if not selected: messagebox.showwarning("No Selection", "Please select documents to download") return def download_all(): format_type = self.format_var.get() save_path = self.save_path_var.get() for i, (doc_id, doc_name) in enumerate(selected): self.status_var.set(f"Downloading {doc_name}...") self.progress_var.set((i / len(selected)) * 100) try: self.download_document(doc_id, doc_name, format_type, save_path) except Exception as e: messagebox.showerror("Download Error", f"Failed to download {doc_name}: {str(e)}") self.status_var.set("Download completed!") self.progress_var.set(100) messagebox.showinfo("Success", f"Downloaded {len(selected)} documents") Thread(target=download_all, daemon=True).start() 'rtf': MimeType.RTF }