-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoperateDocx.py
129 lines (110 loc) · 4.01 KB
/
operateDocx.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!--encoding=utf-8--!
'''
instruction: this script is used to write word document.
dependent: py-docx
'''
from docx import Document
from docx.shared import Inches
'''
reference method, not in use.
'''
def operDocx():
document = Document()
document.add_heading('Document Title', 0)
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'
)
# document.add_picture('monty-truth.png', width=Inches(1.25))
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'
# 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
document.add_page_break()
document.save('demo.docx')
def writeDocx(contentList,docxName):
document = Document()
# p = document.add_paragraph(content)
# document.add_page_break()
total = contentList[0].__len__()
print 'amount of content: '+ str(total)
p = 0
q = 0
hasContent = True
# row = 20
# col = 3
t = 1
while(hasContent):
if p >= total:
hasContent = False
break
# if total - p > 60:
table = document.add_table(rows=20, cols=3)
# else:
# row = (total -p)//3 + 1
# table = document.add_table(rows=20, cols=3)
print 'table added ' + str(t)
num = 1
for i in range(20):
# print 'insert row data'
for j in range(3):
if p >= total:
hasContent = False
break
table.rows[i].cells[j].text = str(num) + ')\t' + contentList[0][p]
num = num +1
p = p + 1
# document.add_paragraph('----------------------------------------------------------------------------------------------------------------------')
# document.add_paragraph('----------------------------------------------------------------------------------------------------------------------')
# table = document.add_table(rows=3, cols=20)
# for m in range(3):
# for n in range(20):
# table.rows[m].cells[n].text = str(q + 1) +')'+ str(contentList[1][q])
# q = q + 1
# results = ''
# num = 1
# for m in range(60):
# results = results +'^'+ str(num)+'>'+str(contentList[1][q])+'$'
# q = q + 1
# num +=1
# par = document.add_paragraph(u'本页答案: ')
# par.add_run(results).italic = True
print 'now in the '+ str(p) + 'step'
t = t +1
# document.add_paragraph('----------------------------------------------------------------------------------------------------------------------')
document.add_page_break()
document.save(docxName)
# def id(id=0):
# return id+1
def saveExpression(Expressions,docxName):
document = Document()
total = len(Expressions)
print 'Total Expressions are: '+str(total)
index=0
while index<total:
table = document.add_table(rows=25, cols=4)
for i in range(25):
for j in range(4):
table.rows[i].cells[j].text = Expressions[index]
index = index+1
document.add_page_break()
document.save(docxName)
if __name__=="__main__":
print 'this is the main method'
operDocx()
# order = id()
# writeDocx( '\t'+str(order) + '.'+ ' 12 + 44 =\t' + str(order) + '.'+ '\t 12 + 44 =\t' + str(order) + '.'+ '\t 12 + 44 =\t'+'\n\r','quiz.docx')