2017-09-22 3 views
0

Ich suche nach einer Möglichkeit, einen Bericht zu formatieren, der mit Python generiert wird. Ich habe bereits Python docx library text align überprüft, aber das hat nicht funktioniert und nicht wirklich meine Bedürfnisse erfüllt.Ausrichten von Text/Formatieren von Text - Docx - Python

Was ich hoffe ist zu tun, einen Absatz Zentrum zu formatieren, wieder links, usw.

from docx import Document 
from docx.enum.section import WD_SECTION 
from docx.enum.text import WD_ALIGN_PARAGRAPH 

document = Document() 
app_name = "Company App Here" 
consultant = "Jerry" 

document.add_heading (app_name + " Report",0) 

document.add_paragraph("Testing performed by " + consultant) #How do I align this to the center? 

document.add_page_break() 

document.add_heading('Executive Summary\n',1) 
document.add_paragraph('''\tThe blah blah blah text here''') #Align left and bold this paragraph 

document.add_page_break() 


document.save('./report.docx') 

Danke,

J

Antwort

1

Dieser Code wird für Sie Run

from docx import Document 
from docx.enum.section import WD_SECTION 
from docx.enum.text import WD_ALIGN_PARAGRAPH 

document = Document() 
app_name = "Company App Here" 
consultant = "Jerry" 

document.add_heading (app_name + " Report",0) 

p1=document.add_paragraph("Testing performed by " + consultant) 
p1.alignment=WD_ALIGN_PARAGRAPH.CENTER 
document.add_page_break() 

document.add_heading('Executive Summary\n',1) 
p2=document.add_paragraph() 
p2.add_run('\tThe blah blah blah text here').bold=True 
p2.alignment=WD_ALIGN_PARAGRAPH.LEFT 
document.add_page_break() 


document.save('./report.docx') 

Dies erzeugt ein docx (gemäß der Anforderung der Frage).

+0

es wird ein docx gemäß der Fragen Anforderung -.- –