Skip to content

Instantly share code, notes, and snippets.

@mxchinegod
Last active January 26, 2023 01:23
Show Gist options
  • Save mxchinegod/94c1e4062206c650627921f882496e80 to your computer and use it in GitHub Desktop.
Save mxchinegod/94c1e4062206c650627921f882496e80 to your computer and use it in GitHub Desktop.
Python script to take rows of an Excel file and put them into a Word Document, by page.
import openpyxl
from docx import Document
def stupid_tax(x):
try:
x = x.split('.')[0]
return x
except:
return x
f = Document()
x = input("What is the filename? (must be .xlsx type): ")
h = input("To what column letter would you like to export?: ")
wb = openpyxl.load_workbook(stupid_tax(x)+'.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
end = str(h)+str(sheet.max_row)
print(end)
for rowOfCellObjects in sheet['A1':end]:
for cellObj in rowOfCellObjects:
f.add_paragraph(str(cellObj.value))
loc = (str(cellObj).split('.'))
if loc[1][0] == str(h[0]):
f.add_page_break()
print("Write successful.\n")
x = input("Name for export?: ")
f.save(stupid_tax(x)+'.docx')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment