Categories We Write About

Build a document revision tracker

Here’s a basic implementation of a Document Revision Tracker using Python. This script keeps a record of all changes made to a document by storing each version along with metadata like timestamp and description.


python
import os import json from datetime import datetime class RevisionTracker: def __init__(self, doc_name): self.doc_name = doc_name self.versions_dir = f"{doc_name}_versions" self.metadata_file = os.path.join(self.versions_dir, "metadata.json") if not os.path.exists(self.versions_dir): os.makedirs(self.versions_dir) with open(self.metadata_file, 'w') as f: json.dump([], f) def save_revision(self, content, description): timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") version_filename = f"{self.doc_name}_{timestamp}.txt" version_path = os.path.join(self.versions_dir, version_filename) # Save the document content with open(version_path, 'w') as file: file.write(content) # Update metadata with open(self.metadata_file, 'r') as f: metadata = json.load(f) metadata.append({ "version": version_filename, "timestamp": timestamp, "description": description }) with open(self.metadata_file, 'w') as f: json.dump(metadata, f, indent=4) print(f"Saved revision: {version_filename}") def list_revisions(self): with open(self.metadata_file, 'r') as f: metadata = json.load(f) for entry in metadata: print(f"{entry['timestamp']} - {entry['version']}: {entry['description']}") def load_revision(self, version_filename): version_path = os.path.join(self.versions_dir, version_filename) if not os.path.exists(version_path): print("Version not found.") return None with open(version_path, 'r') as file: return file.read() # Example Usage: if __name__ == "__main__": tracker = RevisionTracker("project_doc") # Save a new revision tracker.save_revision("This is the initial draft of the document.", "Initial draft") # Save another revision tracker.save_revision("Added introduction and objectives.", "Added intro section") # List all revisions tracker.list_revisions() # Load a specific revision content = tracker.load_revision("project_doc_20250517_121500.txt") if content: print("nLoaded content:n", content)

Features:

  • Stores each revision in a separate .txt file.

  • Maintains a JSON metadata file for tracking versions.

  • Allows loading specific revisions by file name.

  • Easy to integrate into CLI or GUI applications.

Let me know if you need a web-based version, GUI interface, or a Git-like command line wrapper.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About