Skip to content

Commit

Permalink
Documentation on how to render spreadsheets as PDF tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Dec 10, 2024
1 parent 234cc33 commit 6b742cb
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.gz binary
*.pdf binary
*.p12 binary
*.ods binary
*.otf binary
*.ttf binary
*.TTF binary
Expand All @@ -13,3 +14,4 @@
*.gif binary
*.png binary
*.webp binary
*.xlsx binary
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): now parses `<title>` tags to set the [document title](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.set_title). By default, it is added as PDF metadata, but not rendered in the document body. However, this can be enabled by passing `render_title_tag=True` to `FPDF.write_html()`.
* support for LZWDecode compression [issue #1271](https://github.com/py-pdf/fpdf2/issues/1271)
* support for [page labels](https://py-pdf.github.io/fpdf2/PageLabels.html) and created a [reference table of contents](https://py-pdf.github.io/fpdf2/DocumentOutlineAndTableOfContents.html) implementation
* documentation on how to: [render spreadsheets as PDF tables](https://py-pdf.github.io/fpdf2/RenderingSpreadsheetsAsPDFTables.html)
### Fixed
* support for `align=` in [`FPDF.table()`](https://py-pdf.github.io/fpdf2/Tables.html#setting-table-column-widths). Due to this correction, tables are now properly horizontally aligned on the page by default. This was always specified in the documentation, but was not in effect until now. You can revert to have left-aligned tables by passing `align="LEFT"` to `FPDF.table()`.
* `FPDF.set_text_shaping(False)` was broken since version 2.7.8 and is now working properly - [issue #1287](https://github.com/py-pdf/fpdf2/issues/1287)
Expand Down
21 changes: 21 additions & 0 deletions docs/RenderingSpreadsheetsAsPDFTables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Rendering spreadsheets as PDF tables

<img src="table-from-spreadsheet.png" style="max-width: 15rem">

## From a .csv spreadsheet
Example input file: [color_srgb.csv](../tutorial/color_srgb.csv)
```python
{% include "../tutorial/csv2table.py" %}
```

## From a .xlsx spreadsheet
Example input file: [color_srgb.xlsx](../tutorial/color_srgb.xlsx)
```python
{% include "../tutorial/xlsx2table.py" %}
```

## From an .ods spreadsheet
Example input file: [color_srgb.ods](../tutorial/color_srgb.ods)
```python
{% include "../tutorial/ods2table.py" %}
```
Binary file added docs/table-from-spreadsheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ nav:
- 'Combine with pypdf': 'CombineWithPypdf.md'
- 'Combine with pdfrw': 'CombineWithPdfrw.md'
- 'Matplotlib, Pandas, Plotly, Pygal': 'CombineWithChartingLibs.md'
- 'Usage in web APIs': 'UsageInWebAPI.md'
- 'Rendering spreadsheets as PDF tables': 'RenderingSpreadsheetsAsPDFTables.md'
- 'Combine with Rough.js': 'CombineWithRoughJS.md'
- 'Templating with Jinja': 'TemplatingWithJinja.md'
- 'Usage in web APIs': 'UsageInWebAPI.md'
- 'Database storage': 'DatabaseStorage.md'
- 'Development':
- 'Development guidelines': 'Development.md'
Expand Down
17 changes: 17 additions & 0 deletions tutorial/color_srgb.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Name,HEX,RGB
White,#FFFFFF,"rgb(100,100,100)"
Silver,#C0C0C0,"rgb(75,75,75)"
Gray,#808080,"rgb(50,50,50)"
Black,#000000,"rgb(0,0,0)"
Red,#FF0000,"rgb(100,0,0)"
Maroon,#800000,"rgb(50,0,0)"
Yellow,#FFFF00,"rgb(100,100,0)"
Olive,#808000,"rgb(50,50,0)"
Lime,#00FF00,"rgb(0,100,0)"
Green,#008000,"rgb(0,50,0)"
Aqua,#00FFFF,"rgb(0,100,100)"
Teal,#008080,"rgb(0,50,50)"
Blue,#0000FF,"rgb(0,0,100)"
Navy,#000080,"rgb(0,0,50)"
Fuchsia,#FF00FF,"rgb(100,0,100)"
Purple,#800080,"rgb(50,0,50)"
Binary file added tutorial/color_srgb.ods
Binary file not shown.
Binary file added tutorial/color_srgb.xlsx
Binary file not shown.
17 changes: 17 additions & 0 deletions tutorial/csv2table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env python3
# USAGE: ./csv2table.py spreadsheet.csv
import csv, sys
from fpdf import FPDF, FontFace
from fpdf.drawing import color_from_hex_string

pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=22)
with pdf.table() as table:
with open(sys.argv[1], encoding="utf-8") as csv_file:
reader = csv.reader(csv_file, delimiter=",")
for i, row in enumerate(reader):
# We color the row based on the hexadecimal code in the 2nd column:
style = FontFace(fill_color=color_from_hex_string(row[1])) if i > 0 else None
table.row(row, style=style)
pdf.output("from-csv.pdf")

0 comments on commit 6b742cb

Please sign in to comment.