我有以下代码:
table = document.add_table(rows=1, cols=8)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Name'
hdr_cells[1].text = 'Surname'
hdr_cells[2].text = 'Telephone'
(...)
Run Code Online (Sandbox Code Playgroud)
是否有可能使这个细胞加粗?像这样的东西:
hdr_cells[0].text = 'Name'
hdr_cells[1].text = 'Surname'
hdr_cells[2].text = 'Telephone'
hdr_cells[0].text.bold = True
hdr_cells[1].text.bold = True
hdr_cells[2].text.bold = True
Run Code Online (Sandbox Code Playgroud) 我正在使用以下脚本:
header = self.document.add_paragraph(style='Heading 1')
header.style.font.name = 'Arial'
header.style.font.size = Pt(16)
header.add_run('Header One')
Run Code Online (Sandbox Code Playgroud)
结果是“Header One”得到“Calibri”。
我想从word文件中的评论中删除所有个人信息。
删除作者姓名很好,我使用以下内容做到了这一点,
document = Document('sampleFile.docx')
core_properties = document.core_properties
core_properties.author = ""
document.save('new-filename.docx')
Run Code Online (Sandbox Code Playgroud)
但这不是我需要的,我想删除在该单词文件中发表评论的任何人的姓名。
我们手动执行此操作的方法是进入首选项-> 安全性-> 保存时从此文件中删除个人信息
我正在尝试使用 Python 的 docx 模块创建一个 word 文档。但是我无法向它添加表格边框。
我的代码如下:
import docx
from docx import Document
from docx.shared import Pt
doc = Document('C:/Users/Vinny/Desktop/Python/Template.docx')
doc.add_paragraph('Changes:')
doc.add_paragraph('Metrics:')
#add table
table = doc.add_table(rows = 4, cols = 2, style='TableGrid')
doc.save('C:/Users/Vinny/Desktop/Python/rel.docx')
Run Code Online (Sandbox Code Playgroud)
但它抛出错误为:
Traceback (most recent call last):
File "C:\Users\Vinny\Desktop\Python\abc.py", line 14, in <module>
table = doc.add_table(rows = 4, cols = 2, style='TableGrid')
File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\document.py", line 100, in add_table
table.style = style
File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\table.py", line 134, in style
style_or_name, WD_STYLE_TYPE.TABLE
File "C:\Users\Vinny\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\parts\document.py", line 76, in get_style_id
return …Run Code Online (Sandbox Code Playgroud) 我知道我错过了一些简单的东西,但它并没有陷入困境。我知道我必须在每个单独的单元格上设置宽度。
我想在 Word docx 中构建一个表格,其中第一列是 1.2 英寸,第二列是 5.3 英寸。当我尝试以下操作时,第一列是 0.63 英寸,第二列是 1.72 英寸。我为宽度尺寸设置了什么似乎并不重要。我在第一列尝试了 3.0,它仍然显示为 0.63 英寸。我在这里缺少什么?
import docx
doc = docx.Document()
doc.add_heading('Name: ', level=1)
table = doc.add_table(rows=4, cols=2)
table.cell(0,0).width = 1.2
table.cell(1,0).width = 1.2
table.cell(2,0).width = 1.2
table.cell(3,0).width = 1.2
table.cell(0,1).width = 5.3
table.cell(1,1).width = 5.3
table.cell(2,1).width = 5.3
table.cell(3,1).width = 5.3
table.cell(0,0).text = 'Time Zone'
table.cell(1,0).text = 'Link'
table.cell(1,1).text = 'https://www.google.com/'
table.cell(2,0).text = 'Website'
table.cell(3,0).text = 'Facebook'
doc.save('test.docx')
Run Code Online (Sandbox Code Playgroud) 我有一个带有数据表的word文件(.docx),我正在尝试使用该表创建一个pandas数据框,我使用了docx和pandas模块.但我无法创建数据框.
from docx import Document
document = Document('req.docx')
for table in document.tables:
for row in table.rows:
for cell in row.cells:
print (cell.text)
Run Code Online (Sandbox Code Playgroud)
并尝试将表读为df pd.read_table("path of the file")
我可以逐个单元格读取数据,但我想读取整个表格或任何特定的列.提前致谢
我试图让它在工作python-docx:
我可以使用的项目符号列表:
from docx import Document
doc = Document()
p = doc.add_paragraph()
p.style = 'List Bullet'
r = p.add_run()
r.add_text("Item 1")
# Something's gotta come here to get the Sub-Item 1
r = p.add_run()
r.add_text("Item 2")
# Something's gotta come here to get the Sub-Item 2
Run Code Online (Sandbox Code Playgroud)
我认为,在中间添加另一段无济于事,因为从本质上讲,这意味着我将List Bullet使用与其父级相同的格式而不是我想要的类似于孩子的格式来制作另一段。另外,run在同一段中添加另一个也无济于事(我尝试过此操作,弄乱了整个过程..)。有办法吗?
当我跑步时
from docx import Document
Run Code Online (Sandbox Code Playgroud)
我收到错误
ImportError: cannot import name Document
Run Code Online (Sandbox Code Playgroud)
我正在研究 Python 2.7。
出于单元测试的目的,我想检查为 Word 段落生成的 XML 是否符合我在解析 HTML 段落时的预期。
如何提取 XML 本身而不是写入文件、解压缩文件并重新读取它包含的 word/document.xml 文件?
例如
from docx import Document
import bs4
def add_parsed_html_to_paragraph(p, s):
soup = bs4.BeautifulSoup(s)
para = soup.find('p')
for e in para.children:
if type(e) == bs4.element.NavigableString:
r = p.add_run(str(e))
else:
r = p.add_run(e.text)
if e.name == 'sub':
r.font.subscript = True
elif e.name == 'sup':
r.font.superscript = True
title = 'A formula: H<sub>2</sub>O.'
document = Document()
p = document.add_paragraph()
add_parsed_html_to_paragraph(p, title)
# ... Now I want to check p or document …Run Code Online (Sandbox Code Playgroud) 我正在使用该python-docx库来提取 ms word 文档。我可以使用相同的库从 word 文档中获取所有表格。但是,我想将表解析为熊猫数据框,是否有任何内置功能可用于将表解析为数据框,或者我必须手动执行此操作?另外,是否有可能知道表格所在的标题名称?谢谢
from docx import Document
from docx.shared import Inches
document = Document('test.docx')
tabs = document.tables
Run Code Online (Sandbox Code Playgroud) python-docx ×10
python ×8
docx ×4
dataframe ×2
pandas ×2
python-2.7 ×2
ms-word ×1
python-3.6 ×1
python-3.x ×1
xml ×1