Update docs/tech_docs/python/python-docx.md

This commit is contained in:
2024-04-17 05:03:26 +00:00
parent babf16f422
commit ff711b5ee7

View File

@@ -1,3 +1,56 @@
Certainly! Let's streamline the enhancements to focus on a few key visual improvements that will make your document look neat and professional without being too elaborate:
### 1. **Apply Basic Styles to Headings**
This will help distinguish different sections and make your document more readable.
```python
from docx.shared import Pt
# Apply a simple style to headings
for level in range(1, 4):
heading_style = doc.styles['Heading {}'.format(level)]
heading_style.font.name = 'Arial'
heading_style.font.size = Pt(14 if level == 1 else 12 if level == 2 else 10)
heading_style.font.bold = True
```
### 2. **Use Tables for Ingredients**
Organizing ingredients in a table makes it easier for the reader to understand the quantities needed.
```python
# Create a simple table for ingredients
def add_ingredients_table(doc, ingredients):
table = doc.add_table(rows=1, cols=2)
table.style = 'Table Grid'
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Ingredient'
hdr_cells[1].text = 'Quantity'
for ingredient, quantity in ingredients:
row = table.add_row().cells
row[0].text = ingredient
row[1].text = quantity
# Example usage:
add_ingredients_table(doc, [
("Apple Cider Vinegar", "1 cup"),
("Sugar or Honey", "1 tablespoon"),
# Add more ingredients as needed...
])
```
### 3. **Insert Section Breaks for Clarity**
Instead of page breaks, using section breaks can help organize the document without adding too many pages.
```python
# Add a section break
def add_section_break(doc):
doc.add_section()
```
These changes apply a clean and functional style to your document, improving readability and organization without overcomplicating the design. You can adjust the specifics like font sizes and table styles according to your preferences and the overall length of your document.
---
## Reference guide for common use cases with the `python-docx` library:
# `python-docx` Reference Guide