DocTemplify is a Python library that streamlines document generation using Google Docs templates. It enables seamless creation of customized documents such as offers, invoices, and emails by leveraging existing Google Docs templates and filling them with dynamic data.
- Template-based document generation
- Google Docs integration
- Placeholder replacement with custom data
- Optional styling for placeholders
- Easy-to-use API for document creation and management
- Automatic generation of document URLs
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Enable the Google Drive API and Google Docs API for your project.
- In your Google Cloud project, go to "IAM & Admin" > "Service Accounts".
- Click "Create Service Account".
- Enter a name for your service account (e.g., "doctemplify-service").
- Grant this service account the "Editor" role for the project.
- Create a JSON key for this service account and download it.
- Create your template document in Google Docs.
- Click the "Share" button in the top right corner.
- In the "Add people or groups" field, paste the email address of your service account. It should look similar to:
your-service-account-name@your-project-id.iam.gserviceaccount.com
- Set the permission to "Editor".
- Click "Send" to share the document with the service account.
- Clone the repository:
git clone https://github.com/yourusername/DocTemplify.git
- Navigate to the DocTemplify directory:
cd DocTemplify
- Install the package in editable mode:
pip install -e .
DocTemplify supports two types of variables in your Google Docs templates:
- Simple Variables: These are replaced with plain text.
- Styled Variables: These are replaced with text that has specific styling applied.
To create a simple variable, use double curly braces:
{{variable_name}}
Example:
Dear {{client_name}},
Thank you for your interest in our {{product_name}}.
To create a styled variable, use double curly braces with a colon followed by CSS-like styling:
{{variable_name : style1: value1; style2: value2;}}
Example:
{{company_name.main : color: #333333; font-weight: bold; font-size: 18pt; font-family: "Arial"}}
{{company_name.accent : color: #0066cc; font-style: italic; font-size: 16px; font-family: "Georgia"}}
DocTemplify supports styled variables, but it's important to understand how the styling is applied:
- Styles for variables are specified in the data provided when generating the document.
- Any styling specified in the document template placeholders is not applied to the final document.
When providing styles in the data, you can use the following properties:
color
: Text color (e.g.,#FF0000
,rgb(255, 0, 0)
, or color names)background-color
: Background colorfont-weight
:normal
orbold
font-style
:normal
oritalic
font-size
: Size inpt
orpx
font-family
: Font name (must be available in Google Docs)text-decoration
:underline
orline-through
When using DocTemplify, you need to provide a dictionary that matches the variables in your template. For simple variables, use string values. For styled variables, provide a dictionary with 'value' and 'style' keys.
Example:
data = {
"client_name": "John Doe",
"product_name": "Solar Panel System",
"company_name": {
"main": {
"value": "SOLAR",
"style": "color: #333333; font-weight: bold; font-size: 18pt; font-family: 'Arial'"
},
"accent": {
"value": "TECH",
"style": "color: #0066cc; font-style: italic; font-size: 16px; font-family: 'Georgia'"
}
},
"offer_details": {
"system_size": "5 kW",
"panel_count": 15,
"estimated_savings": "$1,200 per year"
},
"footer_text": {
"value": "Thank you for choosing our services!",
"style": "font-size: 10pt; font-style: italic;"
}
}
from doctemplify import GoogleDocsConnector, DocumentGenerator
# Initialize the connector with your service account file
connector = GoogleDocsConnector('path/to/your/service_account.json')
# Create a document generator
generator = DocumentGenerator(connector)
# Generate a document
template_id = 'your_template_document_id'
data = {
"client_name": "John Doe",
"product_name": "Solar Panel System",
# ... other data ...
}
# Generate document and get both ID and URL
new_document_id, new_document_url = generator.generate_document(
template_id,
data,
new_name='Client Offer',
return_url=True
)
print(f"New document created:")
print(f"ID: {new_document_id}")
print(f"URL: {new_document_url}")
The generate_document
method now returns both the document ID and URL when return_url=True
is specified.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.