This repository was archived by the owner on Apr 20, 2026. It is now read-only.
forked from CodeDrome/postgresql-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgtoexcel.py
More file actions
41 lines (30 loc) · 1.26 KB
/
pgtoexcel.py
File metadata and controls
41 lines (30 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import psycopg2
import openpyxl
from openpyxl.styles import Font
def export_to_excel(connection, query_string, headings, filepath):
"""
Exports data from PostgreSQL to an Excel spreadsheet using psycopg2.
Arguments:
connection - an open psycopg2 (this function does not close the connection)
query_string - SQL to get data
headings - list of strings to use as column headings
filepath - path and filename of the Excel file
psycopg2 and file handling errors bubble up to calling code.
"""
cursor = connection.cursor()
cursor.execute(query_string)
data = cursor.fetchall()
cursor.close()
wb = openpyxl.Workbook()
sheet = wb.get_active_sheet()
sheet.row_dimensions[1].font = Font(bold = True)
# Spreadsheet row and column indexes start at 1
# so we use "start = 1" in enumerate so
# we don't need to add 1 to the indexes.
for colno, heading in enumerate(headings, start = 1):
sheet.cell(row = 1, column = colno).value = heading
# This time we use "start = 2" to skip the heading row.
for rowno, row in enumerate(data, start = 2):
for colno, cell_value in enumerate(row, start = 1):
sheet.cell(row = rowno, column = colno).value = cell_value
wb.save(filepath)