python-docx

python-docx center table content

早过忘川 提交于 2019-12-11 08:53:46
问题 I've just started using python-docx and I'm trying to center the contents of my table. I have: table = document.add_table(rows=1, cols=1) tableHeader = table.rows[0].cells tableHeader[0].text = 'test' row_cells = table.add_row().cells row_cells[0].text = 'example' table.style = 'MediumGrid3' which outputs a table with the header test and the text example . I thought table.alignment = 1 would work, but it does nothing. So how do I align all the text to the center? 回答1: The setting you're

python-docx how to merge row cells

十年热恋 提交于 2019-12-11 07:33:30
问题 I am creating a Word document from data using python-docx . I can create all the rows and cells with no problem but, in some cases, when the current record from the database has some content in field comment , I need to add a new line to display a long content. I tried by appending a paragraph, but the result is that the comment is appended after the table, and I need it to be added bellow the current table row. I think the solution is to append a table row with all cells merged, but I can't

python-docx getting docx information

自古美人都是妖i 提交于 2019-12-11 07:19:32
问题 I was trying to copy the whole text from a docx file with all the styles, font information, images and table data. I get the text from the docx file but when I try to get the font information with doc.paragraphs[0].runs[0].style.font.name it shows None . Can anyone help me? 来源: https://stackoverflow.com/questions/53205307/python-docx-getting-docx-information

Python-docx set the text direction RTL

浪尽此生 提交于 2019-12-11 05:49:38
问题 I am trying to create a docunent with RTL (Right-To-Left) text direction def printExam(): #get the exam questions rows = db(db.exam_questions.exam == request.vars.exam).select() # create the documnet document = Document() document.add_heading(u"أختبار", 0) #for row in rows: row = rows[0] run = document.add_paragraph().add_run(str(row.question.questionText).decode( "utf-8" )) font = run.font font.rtl = True I got the following exception: Traceback (most recent call last): File "C:\Users\web2py

Reading .docx files in Python to find strikethrough, bullets and other formats

别等时光非礼了梦想. 提交于 2019-12-11 05:44:50
问题 Can anyone help me identify, in Python using python-docx, if a paragraph in a .docx file contains text that is formatted with strikethrough (ie. it appears but is crossed out), or has a bullet point at the start? I am trying to write a script to identify the structure in a document and parse the content. So far I am able to read a .docx file and iterate over the paragraphs, identifying paragraphs that are bold. from docx import Document document = Document(r'C:\stuff\Document.docx') for p in

Edit content in header of document Python-docx

女生的网名这么多〃 提交于 2019-12-11 05:28:59
问题 I am trying to find and replace text in a Textbox in Header of document. But after searching for awhile, it seems there is no way to access the content in the Header or "float" text boxes via python-docx (I read issue here) So, it means we have to find and replace directly on the xml format of document. Do you know anyway to do that? 回答1: I found a way to solve this problem. For instance, I have a template.docx file, and I want to change text in a Textbox in Header as described above. Flow

python-docx how to apply different styles to different cells in a table

大兔子大兔子 提交于 2019-12-11 05:27:14
问题 Working on a table in python-docx. I am trying to apply a different style to the first row of the table vs. the rest of the rows. This would be the Header row. Here's the table: table2 = doc.add_table(rows=10, cols=3) heading_cells_table2 = table2.rows[0].cells heading_cells_table2[0].text = 'Header 1' heading_cells_table2[1].text = 'Header 2' heading_cells_table2[2].text = 'Header 3' I was hoping I could do something like this: table2.rows[0].style = table_heading_style I've tried a bunch of

Create word document and then attach it to email Django

我怕爱的太早我们不能终老 提交于 2019-12-11 02:50:47
问题 I'm currently using python_docx in order to create Word Documents in Python. What I'm trying to achieve is that I need to create Document File in Django and then attach it to an email using django.core.mail without having to save the file on the server. I've tried creating the Word File using this (taken from an answer also within StackOverflow): def generate(self, title, content): document = Document() docx_title=title document.add_paragraph(content) f = BytesIO() document.save(f) length = f

Python-Docx missing default template

一曲冷凌霜 提交于 2019-12-11 02:44:32
问题 I recently installed the python-docx package and I'm having some trouble with the default template. I'm just setting up a document and have the following code (in a file called maintitle.py): from docx import Document from docx.shared import Inches document = Document() However, I get the following error: Traceback (most recent call last): File "/Users/myname/Desktop/Python/maintitle.py", line 4, in <module> document = Document() File "/Library/Frameworks/Python.framework/Versions/3.6/lib

How to add inter-page links in MS word using python-docx?

送分小仙女□ 提交于 2019-12-10 12:06:49
问题 I am creating a file which contain text data on 1st 4 pages and all images from page 5 onwards. There is a table having page numbers as column. I want to add link to each of the page number in that column by clicking on which it should take me the the image page referenced by that page number. I am creating this document using python-docx. While stumbling on google I got a solution for creating hyperlink using python-docx. Clicking on the text with hyperlink takes me to url referenced by it.