Created
September 29, 2015 08:53
-
-
Save cindygis/46d31815451e19836776 to your computer and use it in GitHub Desktop.
Writes the contents of all rows from multiple tables in a Word document to a spreadsheet.
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
import docx | |
import xlwt | |
doc = r"C:\Some\Arb\Folder\input.docx" | |
xls = r"C:\Some\Arb\Folder\output.xls" | |
document = docx.Document(doc) | |
book = xlwt.Workbook() | |
cur_sheet = book.add_sheet("Tables") | |
row_num = 0 | |
tables = document.tables # Get all the tables in the docx | |
# Get the header row from the 1st table's 1st row | |
for index, cell in enumerate(tables[0].rows[0].cells): | |
cur_sheet.write(row_num, index, cell.text) | |
for table in tables: | |
for row in table.rows[1:]: # Skip the repeating header row of each table | |
row_num += 1 | |
for index, cell in enumerate(row.cells): | |
if cell != '': | |
cur_sheet.write(row_num, index, cell.text.strip()) | |
book.save(xls) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment