Created
February 6, 2017 20:38
-
-
Save xie186/8738e24ea18a054ec49bdad6d61b81f9 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Jan 26 10:55:55 2017 | |
@author: xie186 | |
""" | |
#import urllib3.request | |
from docx import Document | |
from docx.shared import Inches | |
from docx.oxml.shared import OxmlElement, qn | |
from docx.enum.text import WD_ALIGN_PARAGRAPH | |
from docx.oxml import parse_xml ## for table backgroud | |
from docx.oxml.ns import nsdecls ## for table bakcgroud | |
##http://stackoverflow.com/questions/21393758/unicodedecodeerror-ascii-codec-cant-decode-byte-0xe5-in-position-0-ordinal | |
#import sys | |
#reload(sys) | |
#sys.setdefaultencoding("utf-8") | |
document = Document("template.docx") | |
for tab in document.tables: | |
print "XX" | |
print tab | |
head_name = '基因检测报告' | |
head_name = head_name.decode('utf-8') | |
header = document.add_heading(head_name, 0) | |
#header = document.add_eading(u'基因检测报告', 0) | |
header.alignment = WD_ALIGN_PARAGRAPH.CENTER | |
################# Table of content############################ | |
paragraph = document.add_paragraph() | |
run = paragraph.add_run() | |
fldChar = OxmlElement('w:fldChar') # creates a new element | |
fldChar.set(qn('w:fldCharType'), 'begin') # sets attribute on element | |
instrText = OxmlElement('w:instrText') | |
instrText.set(qn('xml:space'), 'preserve') # sets attribute on element | |
instrText.text = 'TOC \o "1-3" \h \z \u' # change 1-3 depending on heading levels you need | |
fldChar2 = OxmlElement('w:fldChar') | |
fldChar2.set(qn('w:fldCharType'), 'separate') | |
fldChar3 = OxmlElement('w:t') | |
fldChar3.text = "Right-click to update field." | |
fldChar2.append(fldChar3) | |
fldChar4 = OxmlElement('w:fldChar') | |
fldChar4.set(qn('w:fldCharType'), 'end') | |
r_element = run._r | |
r_element.append(fldChar) | |
r_element.append(instrText) | |
r_element.append(fldChar2) | |
r_element.append(fldChar4) | |
p_element = paragraph._p | |
##################################################################### | |
p = document.add_paragraph('A plain paragraph having some ') | |
p.add_run('bold').bold = True | |
p.add_run(' and some ') | |
p.add_run('italic.').italic = True | |
document.add_heading('Heading, level 1', level=1) | |
document.add_paragraph('Intense quote', style='IntenseQuote') | |
document.add_paragraph( | |
'first item in unordered list', style='ListBullet' | |
) | |
document.add_paragraph( | |
'first item in ordered list', style='ListNumber' | |
) | |
#urllib3.request.urlretrieve("http://placehold.it/350x150", "placeholder.png") | |
document.add_picture('monty-truth.png', width=Inches(1.25)) | |
recordset = [ | |
{ | |
"id" : 1, | |
"qty": 2, | |
"desc": "New item" | |
}, | |
{ | |
"id" : 2, | |
"qty": 2, | |
"desc": "New item" | |
}, | |
{ | |
"id" : 3, | |
"qty": 2, | |
"desc": "New item" | |
}, | |
] | |
table = document.add_table(rows=1, cols=3) | |
hdr_cells = table.rows[0].cells | |
hdr_cells[0].text = 'Qty' | |
hdr_cells[1].text = 'Id' | |
hdr_cells[2].text = 'Desc' | |
shading_elm = parse_xml(r'<w:shd {} w:fill="dce3db"/>'.format(nsdecls('w'))) | |
hdr_cells[0]._tc.get_or_add_tcPr().append(shading_elm) | |
for item in recordset: | |
row_cells = table.add_row().cells | |
row_cells[0].text = str(item["qty"]) | |
row_cells[1].text = str(item["id"]) | |
row_cells[2].text = item["desc"] | |
## http://python-docx.readthedocs.io/en/latest/user/styles-understanding.html | |
table.style = 'TableGrid' | |
#table.style = 'LightShading-Accent4' | |
#table.style = 'LightList-Accent4' | |
#table.stype = 'ColorfulShading' | |
document.add_page_break() | |
############################## Insert caption ######### | |
from docx.oxml import OxmlElement | |
from docx.oxml.ns import qn | |
from docx.text.run import Run | |
# Configure | |
style = 'Figure' # Can be 'Figure', 'Table' or 'Equation' | |
num = 1 | |
# Initialize | |
d = Document() | |
p1 = d.add_paragraph("Hi please cross-references:") | |
p = d.add_paragraph() | |
# Insert caption | |
elem = p._element | |
r = OxmlElement('w:r') | |
elem.append(r) | |
Run(r, p).text = style + ' ' | |
# Insert caption number | |
pCaption = OxmlElement('w:fldSimple') | |
pCaption.set(qn('w:instr'), r' SEQ {style} \* ARABIC '.format(style=style)) | |
elem.append(pCaption) | |
r = OxmlElement('w:r') | |
pCaption.append(r) | |
rPr = OxmlElement('w:rPr') | |
r.append(rPr) | |
rPr.append(OxmlElement('w:noProof')) | |
Run(r, p).text = str(num) | |
p.add_run(' XTEST') | |
### | |
d.save("xx.docx") | |
document.save('demo.docx') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment