Python Docx-如何编号标题?

时间:2018-12-20 14:19:29

标签: python python-docx

Python Docx有一个不错的example

我使用了多个def batch_generator(batch_size = 64): while True: pos = positiveImg[np.random.choice(len(positiveImg), batch_size)] neg = negativeImg[np.random.choice(len(negativeImg), batch_size)] anc = anchorsImg[np.random.choice(len(anchorsImg), batch_size)] x_data = {'inp1': anc, 'inp2': pos, 'inp3': neg } y_data = {'y1': np.zeros((64,0)), 'y2': np.zeros((64,0)), 'y3': np.zeros((64,0))} yield (x_data, y_data) def triplet_loss(y_true, y_pred): anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2] pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis=-1) neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis=-1) basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), 0.2) loss = tf.reduce_sum(tf.maximum(basic_loss, 0.0)) return loss def getModels(): FRmodel = keras.models.load_model('FR.h5', custom_objects={'triplet_loss': triplet_loss}) inp1 = Input((3, 96, 96), name= 'inp1') inp2 = Input((3, 96, 96), name= 'inp2') inp3 = Input((3, 96, 96), name= 'inp3') pred1 = FRmodel(inp1) pred2 = FRmodel(inp2) pred3 = FRmodel(inp3) inputs = [inp1, inp2, inp3] outputs = [pred1, pred2, pred3] model = keras.models.Model(inputs=[inp1, inp2, inp3], outputs= [pred1, pred2, pred3]) return FRmodel, model generator = batch_generator(64) FRmodel, my_model = getModels() my_model.compile(optimizer = 'adam', loss = triplet_loss, metrics = ['accuracy']) my_model.fit_generator(generator, epochs=5,steps_per_epoch=30) ,当我在MS Word中打开生成的文档时可以看到级别正确。

我看不到的是编号,例如1、1.1、1.1.1,等等,我只是看到标题文本。

如何使用Docx显示标题编号?

3 个答案:

答案 0 :(得分:3)

字母数字标题前缀是根据轮廓样式和标题级别自动创建的。设置轮廓样式并插入正确的级别,您将获得编号。

编号样式尚未实现。从文档: _NumberingStyle对象 类docx.styles.style._NumberingStyle [source] 编号样式。尚未实施。

但是,如果设置了标题(例如,section.style = document.styles ['Heading 1']),则应默认使用该标题的潜在编号样式。

答案 1 :(得分:0)

此答案确实可以为您提供帮助

首先,您需要像这样新建一个不带数字的标头

paragraph = document.add_paragraph()
paragraph.style = document.styles['Heading 4']

然后您将拥有这样的xml字

<w:pPr>
<w:pStyle w:val="4"/>
</w:pPr>

然后,您可以访问xml字词“ pStyle”属性,并使用下面的代码对其进行更改

header._p.pPr.pStyle.set(qn('w:val'), u'4FDD')

最终打开单词文件,您将得到想要的!!!

答案 2 :(得分:0)

def __str__(self):
    if self.nivel == 1: 
        return str(Level.count_1)+'.- '+self.titulo
    elif self.nivel==2: #Imprime si es del nivel 2
        return str(Level.count_1)+'.'+str(Level.count_2)+'.- '+self.titulo
    elif self.nivel==3: #Imprime si es del nivel 3
        return str(Level.count_1)+'.'+str(Level.count_2)+'.'+str(Level.count_3)+'.- '+self.titulo