My problem is that when I apply the strike-through or double strike-through formatting and save the file it is not reflected in output file.
Following code does not do the trick:
from docx import Document
document = Document()
p = document.add_paragraph()
p.add_run('Strike through the following text').strike = True
document.save('demo.docx')
This should do the job:
p.add_run('Strike through the following text').font.strike = True
strike is a property of the font object : docs
EDIT
If multiple font properties were to be changed the code should be :
sentence = p.add_run('Strike through the following text')
sentence.font.strike = True
sentence.font.name = 'Comic Sans MS'
Related
Because the original project seems inactive I have to replicate my Issue here at StackOverflow.
It seems to me that the global document style does overwrite lower styles e.g. TableStyle. I assume I misunderstand the concept here.
See this code as an example
#!/usr/bin/env python3
import os
import docx
document = docx.Document()
document.styles['Normal'].font.name = 'Fira Mono'
document.styles['Normal'].font.size = docx.shared.Pt(8)
my_tab_style = document.styles.add_style('MyTab', docx.enum.style.WD_STYLE_TYPE.TABLE)
my_tab_style.font.name = 'Fira Sans'
my_tab_style.font.size = docx.shared.Pt(20)
p = document.add_paragraph('Should be Fira Mono 8pt')
tab = document.add_table(rows=1, cols=1, style='MyTab')
tab.rows[0].cells[0].text = 'Should be Fira Sans 20pt'
document.save('test.docx')
os.system('start test.docx')
I would expect the document text in "Fira Mono 8pt" and the table content in "Fira Sans 20pt". But it isn't.
When you outcomment the document style lines
# document.styles['Normal'].font.name = 'Fira Mono'
# document.styles['Normal'].font.size = docx.shared.Pt(8)
the table style is taken into account.
yes i know many questions about this issue exists but i couldn't make any of them work.
i have python 3.7 and python-docx 0.8.11.
i have tried many solutions including this one
from docx import Document, enum
document = Document()
mystyle = document.styles.add_style('mystyle', enum.style.WD_STYLE_TYPE.CHARACTER)
run = document.add_paragraph().add_run(text)
run.style = mystyle
font = run.font
font.rtl = True
document.save('test.docx')
also
from docx import Document, enum
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
doc = Document()
rtlstyle = doc.styles.add_style('rtl', enum.style.WD_STYLE_TYPE.PARAGRAPH)
rtlstyle.font.rtl = True
p = doc.add_paragraph(text)
p.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
p.style = rtlstyle
doc.save('test.docx')
nothing worked so far
see this : https://stackoverflow.com/posts/73133074/revisions
also as mensiond here put this before your text: u'\u202B' + "your text"
or u'\u202E' if didn't work
I would like to change the font size of the title and of the body of my pptx-presentation. I tried to set it via title_shape.font = Pt(15) and body_shape.font = Pt(10), which does not work.
Here is my code:
from pptx import Presentation, util, text
from pptx.util import Cm, Pt
import fnmatch
import os
import contentOf_pptx as contOfPres
# ..............
# Generate presentation
# ..............
prs = Presentation()
#blank_slide_layout = prs.slide_layouts[6] #blank layout, see slide layout in powerpoint
title_only = prs.slide_layouts[5] #title only, see slide layout in powerpoint
# ..............
# set layout
# ..............
bullet_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title
title_shape.font = Pt(15)
body_shape = shapes.placeholders[1]
body_shape.font = Pt(10)
# ..............
# set relevant text objects
# ..............
title_shape.text = 'Test Title'
tf = body_shape.text_frame
tf.text = 'Test SubText'
# ----------------------------------
# Store pptx
# ----------------------------------
prs.save('C:\\tests\\test_pptx_python.pptx')
A Shape object does not have a .font attribute (at least it didn't until you added one by assigning to that name :)
Font is characteristic of a Run object; a run is a sequence of characters that all share the same character formatting, also loosely known as a font.
A Paragraph object also has a .font property which is used the same way, but which specifies the default font for the runs in a paragraph. Individual runs in that paragraph can override that default by setting the attributes of their own font object.
If you only want one font for the shape (which is common), probably the fastest way is:
shape.text_frame.paragraphs[0].font.size = Pt(15)
which works because most shapes only contain a single paragraph (and all must contain at least one).
More thorough would be:
for paragraph in shape.text_frame.paragraphs:
paragraph.font.size = Pt(15)
and more thorough still would be:
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(15)
More details on this are in the documentation here:
https://python-pptx.readthedocs.io/en/latest/user/text.html
Here is a simple approach that worked for me:
slide = prs.slides.add_slide(blank_slide_layout)
slide.shapes.title.text = "The Title of My Slide"
slide.shapes.title.text_frame.paragraphs[0].font.size = Pt(15)
This is what I have tried:
from docx import Document
from docx.shared import Inches
from docx.shared import Pt
table = document.add_table(rows= 3, cols = 1)
row = table.rows[0]
runner = row.cells[0].paragraphs[0].add_run('Summary')
runner.bold = True
runner.small_caps = True
document.save('demo.docx')
I am trying to create a table and small caps the text but cannot get the syntax right
I have figured how to use small caps for text.
runner_font = runner.font
runner_font.size = Pt(14)
runner_font.small_caps = True
runner_font.bold = True
I believe bold and italic are properties that built in under both add_run and font. But for small_caps, all_caps, color can be called under font only.
I build PDF using ReportLab. My program has a MyDocTemplate(SimpleDocTemplate) class with two methods: beforePage(self) and afterPage(self) which add header and footer (as PNG image) on every page. There is also a MyDocStyle class which describe ParagraphStyle.
Main method looks like this:
TITLE = Paragraph(Title, MyDocStyle.h1)
TO = Paragraph(To, MyDocStyle.h2)
FROM = Paragraph(From, MyDocStyle.h2)
SUBJECT = Paragraph(Subject, MyDocStyle.h2)
LONG_PARAGRAPH = Paragraph(Text, MyDocStyle.h3)
...
Elements = [TITLE, TO, FROM, SUBJECT, LONG_PARAGRAPH, ...]
doc = MyDocTemplete('output.pdf', pagesize=A4,
leftMargin=2*cm, rightMargin=2*cm,
topMargin=4*cm, bottomMargin=4*cm)
doc.build(Elements)
Data comes from CSV files and GUI. From time to time (depends on data length) I receive an error:
Flowable <Spacer at 0x2631120 frame=normal>...(1 x 5.66929133858) too large
on page 1 in frame 'normal'(469.88976378 x 603.118110236) of template 'First'
This exception stop my program. For short Paragraphs I set in MyDocStyle class h2.keepWithNext = 1 however it's not perfect solution. ReportLab split correctly long paragraph if end of paragraph does not "coincide" with end of page (text area).
How can I deal with it?
This error occurs when ReportLab try to split a Spacer over two pages. It seems that the only way to workaround this issue is wrap your Spacer into a KeepTogether element:
elements.append(KeepTogether(Spacer(width, height)))
Solved. Don't use Spacer (e.g. Spacer(1, 0.2*cm)) as a separator for Paragraph. Instead, define spaceBefore and spaceAfter in ParagraphStyle, for example:
ParagraphStyle(name = 'Normal',
fontName = "Verdana",
fontSize = 11,
leading = 15,
alignment = TA_JUSTIFY,
allowOrphans = 0,
spaceBefore = 20,
spaceAfter = 20,
wordWrap = 1)