使用python win32com无法在MS Word 2007中制作两个单独的表格

发布于 2024-10-15 11:05:22 字数 1259 浏览 11 评论 0原文

我正在尝试使用 Python 在新的 Microsoft Word 文档中创建多个表。我可以创建第一个表。但我认为我的 COM Range 对象配置错误。它并不是指向终点。第一个表格放在“Hello I am a text!”之前,第二个表格放在第一个表格的第一个单元格内。我认为从 wordapp 返回 Range 将返回完整范围,然后使用 wdCollapseStart Enum 折叠它,我认为它是 1。(我在 Python win32com 中找不到常量。)。因此,将表格添加到范围的末尾会将其添加到文档的末尾,但这并没有发生。

有什么想法吗?

谢谢蒂姆

import win32com.client

wordapp = win32com.client.Dispatch("Word.Application") 
wordapp.Visible = 1 
worddoc = wordapp.Documents.Add()
worddoc.PageSetup.Orientation = 1 
worddoc.PageSetup.BookFoldPrinting = 1 
worddoc.Content.Font.Size = 11
worddoc.Content.Paragraphs.TabStops.Add (100)
worddoc.Content.Text = "Hello, I am a text!"

location = worddoc.Range()
location.Collapse(1)
location.Paragraphs.Add()
location.Collapse(1)
table = location.Tables.Add (location, 3, 4)
table.ApplyStyleHeadingRows = 1
table.AutoFormat(16)
table.Cell(1,1).Range.InsertAfter("Teacher")

location1 = worddoc.Range()
location1.Paragraphs.Add()
location1.Collapse(1)
table = location1.Tables.Add (location1, 3, 4)
table.ApplyStyleHeadingRows = 1
table.AutoFormat(16)
table.Cell(1,1).Range.InsertAfter("Teacher1")
worddoc.Content.MoveEnd
worddoc.Close() # Close the Word Document (a save-Dialog pops up)
wordapp.Quit() # Close the Word Application

I am trying to create multiple tables in a new Microsoft Word document using Python. I can create the first table okay. But I think I have the COM Range object configured wrong. It is not pointing to the end. The first table is put before "Hello I am a text!", the second table is put inside the first table's first cell. I thought that returning a Range from wordapp will return the full range, then collapse it using wdCollapseStart Enum which I think is 1. (I can't find the constants in Python win32com.). So adding a table to the end of the Range will add it to the end of the document but that is not happening.

Any ideas?

Thanks Tim

import win32com.client

wordapp = win32com.client.Dispatch("Word.Application") 
wordapp.Visible = 1 
worddoc = wordapp.Documents.Add()
worddoc.PageSetup.Orientation = 1 
worddoc.PageSetup.BookFoldPrinting = 1 
worddoc.Content.Font.Size = 11
worddoc.Content.Paragraphs.TabStops.Add (100)
worddoc.Content.Text = "Hello, I am a text!"

location = worddoc.Range()
location.Collapse(1)
location.Paragraphs.Add()
location.Collapse(1)
table = location.Tables.Add (location, 3, 4)
table.ApplyStyleHeadingRows = 1
table.AutoFormat(16)
table.Cell(1,1).Range.InsertAfter("Teacher")

location1 = worddoc.Range()
location1.Paragraphs.Add()
location1.Collapse(1)
table = location1.Tables.Add (location1, 3, 4)
table.ApplyStyleHeadingRows = 1
table.AutoFormat(16)
table.Cell(1,1).Range.InsertAfter("Teacher1")
worddoc.Content.MoveEnd
worddoc.Close() # Close the Word Document (a save-Dialog pops up)
wordapp.Quit() # Close the Word Application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凑诗 2024-10-22 11:05:22

问题似乎出在代表文档一部分的 Range 对象中。在我的原始代码中,Range 对象包含第一个单元格,并从第一个单元格开始,它将插入其中。相反,我想在范围的末尾插入。所以我得到了以下代码替换来工作。我在 Add() 调用之后移动了 Collapse,并给它一个参数 0。现在每个 Range 对象只有一次 Collapse 调用。

location = worddoc.Range()
location.Paragraphs.Add()
location.Collapse(0)

现在代码可以工作了,我可以从数据库中读取数据并从每个条目填充新表。

蒂姆

The problem seems to be in the Range object that represents a part of the document. In my original code the Range object contains the first cell and starts at the first cell, where it will insert. Instead I want to insert at the end of the range. So I got the following code replacement to work. I moved the Collapse after the Add() call and gave it an argument of 0. Now there is only one Collapse call per Range object.

location = worddoc.Range()
location.Paragraphs.Add()
location.Collapse(0)

Now the code works, I can read from a database and populate new tables from each entry.

Tim

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文