cannot make text rtl with python-docx - python

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

Related

TableStyle is overwritten by document style

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.

python-docx Bilingual Document RTL and LTR

I want to create a bilingual MS word document; containing English and Arabic text.
As you may know, Arabic is an RTL language (written from right to left).
My most difficult problem was solved, Arabic text renders from right-to-left; yet the paragraph alignment stays Left, and I want it right (while keeping English left as it's the default).
Here is my working:
from docx import Document, enum
from docx.shared import Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_PARAGRAPH_ALIGNMENT
document = Document()
document.add_heading('Document Title', 0)
p = document.add_paragraph('English text renders correctly; left to right; with default Left Alignment ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True
mystyle = document.styles.add_style('mystyle', enum.style.WD_STYLE_TYPE.CHARACTER)
paragraph = document.add_paragraph()
paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
p = document.add_paragraph().add_run('هذه فقرة باللغة العربية يجب أن تكتب من اليمين إلى اليسار.')
p.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
font = p.font
font.rtl = True
p.style = mystyle
document.save('demo8.docx')
This is how it should be

How to apply strike-through using python-docx

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'

Creating a table in python docx and use small_caps (python-docx)

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.

How to add a link to a Visio document via Python

I googled for some code to create Visio documents via Python. I want to add shapes, and have hyperlinks. So that you can click on the shape, or preferably on the text inside the shape, and get to a URL.
import os
import win32com.client
from win32com.client import constants
appVisio = win32com.client.Dispatch("Visio.Application")
appVisio.Visible =1
doc = appVisio.Documents.Add("Basic Diagram.vst")
pagObj = doc.Pages.Item(1)
stnObj = appVisio.Documents("Basic Shapes.vss")
mastObj = stnObj.Masters("Rectangle")
shpObj1 = pagObj.Drop(mastObj, 4.25, 5.5)
shpObj1.Text = "This is some text."
shpObj2 = pagObj.Drop(mastObj, 2, 2)
shpObj2.Text = """This is some more text. {\field{\*\fldinst HYPERLINK "http://www.google.com/"}{\fldrslt http://www.google.com}}"""
connectorMaster = appVisio.Application.ConnectorToolDataObject
connector = pagObj.Drop(connectorMaster, 0, 0)
connector.Cells("BeginX").GlueTo(shpObj1.Cells("PinX"))
connector.Cells("EndX").GlueTo(shpObj2.Cells("PinX"))
doc.SaveAs(r'C:\utils\MyDrawing.vsd')
doc.Close()
appVisio.Visible =0
appVisio.Quit()
The RTF link is ignored - I tried that. Visio can add hyperlinks in the UI. So... does anyone know how I can add a link via Python here?
Visio only supports links on a shape (rather than within the text itself). A shape has Hyperlinks collection of Hyperlink objects and so you can add as follows:
# shpObj2.Text = """This is some more text. {\field{\*\fldinst HYPERLINK "http://www.google.com/"}{\fldrslt http://www.google.com}}"""
shpObj2.Text = "This is a shape with multiple links."
shp2Hyperlink1 = shpObj2.Hyperlinks.Add()
shp2Hyperlink1.Name = "Google"
shp2Hyperlink1.Address = "http://www.google.com"
shp2Hyperlink2 = shpObj2.Hyperlinks.Add()
shp2Hyperlink2.Name = "BBC"
shp2Hyperlink2.Address = "http://www.bbc.co.uk"
Under the covers this is just writing cells into the ShapeSheet:

Categories

Resources