Skip to content

Instantly share code, notes, and snippets.

@garymanley
Created December 30, 2017 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garymanley/20322319377d57d0492077f2a03ee7f6 to your computer and use it in GitHub Desktop.
Save garymanley/20322319377d57d0492077f2a03ee7f6 to your computer and use it in GitHub Desktop.
Pandas as word table
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 30 13:03:12 2017
@author: garym
"""
### run imports
import pandas as pd
import pyodbc as py
from docx import Document
from docx.shared import Cm , Pt
## Set up a column widths function
def set_column_width(column, width):
for cell in column.cells:
cell.width = width
## Set up connection string and connection
conn_str = (
r'Driver={SQL Server};'
r'Server=localhost\SQLEXPRESS;'
r'Database=RUNNING;'
r'Trusted_Connection=yes;'
)
cnxn = py.connect(conn_str)
cursor = cnxn.cursor()
## Run query into a dataframe
dftop = pd.read_sql("select top 10 [user], len_min, track_name, track_length from v_history order by track_length desc" , cnxn )
## Create new document
document = Document()
## set up font
font = document.styles['Normal'].font
font.name = 'Arial'
font.size = Pt(10)
## set up margins
sections = document.sections
for section in sections:
section.top_margin = Cm(1.25)
section.bottom_margin = Cm(1.25)
section.left_margin = Cm(1.75)
section.right_margin = Cm(1.75)
## Add new Title
p = document.add_paragraph()
p.add_run('Top 10 Runs by Distance').bold = True
### Add new table to document with shapte of the dataframe
table = document.add_table(dftop.shape[0]+1, dftop.shape[1])
## set the style of the table
table.style = 'Light List Accent 4'
# add the header rows.
for j in range(dftop.shape[-1]):
table.cell(0,j).text = dftop.columns[j]
# add the rest of the data frame
for i in range(dftop.shape[0]):
for j in range(dftop.shape[-1]):
table.cell(i+1,j).text = str(dftop.values[i,j])
## Adjust the column widths using function set up earlier
set_column_width(table.columns[0], Cm(2.75))
set_column_width(table.columns[1], Cm(1.75))
set_column_width(table.columns[2], Cm(8.75))
set_column_width(table.columns[0], Cm(3.75))
## set up filename
filenamedocx = r'C:\Users\garym\Documents\PyWinAutoBlog\File.docx'
## save file
document.save(filenamedocx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment