I am trying use python docx to make table in the firs page but when I trying to modify the font size of the title the table size will be change also, even I tried put one more font size change in table setting. How can I do it?
def Firstpage():
titlename='Test'+' '+lastday
title = doc.add_paragraph()
title.add_run(titlename).bold=True
#title.style.font.size=Pt(20)
title.alignment=WD_ALIGN_PARAGRAPH.CENTER
filename=lastday+' '+'test.docx'
filepath = os.path.join(r'C:\Users\Administrator\Desktop\python test\update_test', filename)
doc.save(filepath)
def ShiftTable():
table = doc.add_table(rows=27, cols=6)
table.style='Table Grid'
table.style.font.size=Pt(2)
table.cell(0,0).text = "Shift"
table.cell(0,1).text = "Hour"
table.cell(0,2).text = "Parts Production"
table.cell(0,3).text = "Good Parts"
table.cell(0,4).text = "Bad Parts"
table.cell(0,5).text = "Average Cycle Time"
table.cell(1,0).text = "Morning Shift"
table.cell(1,1).text = "08AM - 09AM"
table.cell(2,1).text = "09AM - 10AM"
table.cell(3,1).text = "10AM - 11AM"
table.cell(4,1).text = "11AM - 12PM"
table.cell(5,1).text = "12PM - 01PM"
table.cell(6,1).text = "01PM - 02PM"
table.cell(7,1).text = "02PM - 03PM"
table.cell(8,0).text = "Shift Summary"
table.cell(9,0).text = "Afternoon Shift"
table.cell(9,1).text = "03PM - 04PM"
table.cell(10,1).text = "04PM - 05PM"
table.cell(11,1).text = "05PM - 06PM"
table.cell(12,1).text = "06PM - 07PM"
table.cell(13,1).text = "07PM - 08PM"
for row in table.rows:
for cell in row.cells:
paragraphs = cell.paragraphs
for paragraph in paragraphs:
for run in paragraph.runs:
font = run.font
font.size= Pt(10)
Firstpage()
ShiftTable()
filename=lastday+' '+'test.docx'
filepath = os.path.join(r'C:\Users\Administrator\Desktop\python test\update_test', filename)
doc.save(filepath)
Now The font size in table is change to 10 but the empty cell is still 20
Font size is applied at the run level. This is how different "runs" of text in a paragraph can have different sizes.
You generally should not change the style settings except where you want those to apply globally. In any case, a table style font setting may or may not be applied to all the text it contains.
If you want to change the text of an individual cell, you must apply that size change to all the runs it contains. In your case you can do that like this:
cell = table.cell(0, 0)
cell.text = 'Shift'
cell_font = cell.text_frame.paragraphs[0].runs[0].font
cell_font.size = Pt(10)
Make sure you understand this part of the documentation having to do with styles:
http://python-docx.readthedocs.io/en/latest/user/styles-understanding.html and
http://python-docx.readthedocs.io/en/latest/user/styles-using.html