forked from reingart/pyfpdf
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation on how to render spreadsheets as PDF tables (#1327)
- Loading branch information
Showing
11 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" %} | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/env python3 | ||
# Script Dependencies: | ||
# odfpy | ||
# USAGE: ./ods2table.py spreadsheet.ods | ||
import sys | ||
from fpdf import FPDF, FontFace | ||
from fpdf.drawing import color_from_hex_string | ||
from odf.opendocument import load | ||
from odf.table import Table, TableCell, TableRow | ||
|
||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=22) | ||
ods = load(sys.argv[1]) | ||
for sheet in ods.getElementsByType(Table): | ||
with pdf.table() as table: | ||
for i, row in enumerate(sheet.getElementsByType(TableRow)): | ||
row = [str(cell) for cell in row.getElementsByType(TableCell)] | ||
# 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-ods.pdf") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env python3 | ||
# Script Dependencies: | ||
# openxlsx | ||
# USAGE: ./xlsx2table.py spreadsheet.xlsx | ||
import sys | ||
from fpdf import FPDF, FontFace | ||
from fpdf.drawing import color_from_hex_string | ||
from openpyxl import load_workbook | ||
|
||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Times", size=22) | ||
wb = load_workbook(sys.argv[1]) | ||
ws = wb.active | ||
with pdf.table() as table: | ||
for i, row in enumerate(ws.rows): | ||
# We color the row based on the hexadecimal code in the 2nd column: | ||
style = FontFace(fill_color=color_from_hex_string(row[1].value)) if i > 0 else None | ||
table.row([cell.value for cell in row], style=style) | ||
pdf.output("from-xlsx.pdf") |