捕获和转换Web的工具

将URL和HTML转换为DOCXPython API

添加转换HTML或网页的功能 into将Word文档发送到您的应用程序从未如此简单 GrabzIt的Python API。 但是,在开始之前,请记住 URLToDOCX, HTMLToDOCX or FileToDOCX 方法 Save or SaveTo 必须调用方法才能实际创建DOCX。

基本选项

当DOCX转换整个网页时捕获网页 int可以包含许多页面的Word文档。 只需一个参数即可转换网页 int文字文件或 将HTML转换为DOCX 如以下示例所示。

grabzIt.URLToDOCX("https://www.tesla.com")
# Then call the Save or SaveTo method
grabzIt.HTMLToDOCX("<html><body><h1>Hello World!</h1></body></html>")
# Then call the Save or SaveTo method
grabzIt.FileToDOCX("example.html")
# Then call the Save or SaveTo method

自订识别码

您可以将自定义标识符传递给 docx文档 方法,如下所示,然后将该值返回到您的GrabzIt Python处理程序。 例如,此自定义标识符可以是数据库标识符,从而允许DOCX文档与特定数据库记录相关联。

from GrabzIt import GrabzItDOCXOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItDOCXOptions.GrabzItDOCXOptions()
options.customId = "123456"

grabzIt.URLToDOCX("https://www.tesla.com", options)
# Then call the Save method
grabzIt.Save("http://www.example.com/handler.py")
from GrabzIt import GrabzItDOCXOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItDOCXOptions.GrabzItDOCXOptions()
options.customId = "123456"

grabzIt.HTMLToDOCX("<html><body><h1>Hello World!</h1></body></html>", options)
# Then call the Save method
grabzIt.Save("http://www.example.com/handler.py")
from GrabzIt import GrabzItDOCXOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItDOCXOptions.GrabzItDOCXOptions()
options.customId = "123456"

grabzIt.FileToDOCX("example.html", options)
# Then call the Save method
grabzIt.Save("http://www.example.com/handler.py")

页眉和页脚

要将页眉或页脚添加到Word文档中,可以请求您要应用特定的 模板 生成的DOCX。 该模板必须是 saved并会指定页眉和页脚的内容以及任何特殊变量。 在下面的示例代码中,用户正在使用他们创建的名为“我的模板”的模板。

from GrabzIt import GrabzItDOCXOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItDOCXOptions.GrabzItDOCXOptions()
options.templateId = "my template"

grabzIt.URLToDOCX("https://www.tesla.com", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.docx")
from GrabzIt import GrabzItDOCXOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItDOCXOptions.GrabzItDOCXOptions()
options.templateId = "my template"

grabzIt.HTMLToDOCX("<html><body><h1>Hello World!</h1></body></html>", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.docx")
from GrabzIt import GrabzItDOCXOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItDOCXOptions.GrabzItDOCXOptions()
options.templateId = "my template"

grabzIt.FileToDOCX("example.html", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.docx")

将HTML元素转换为DOCX

如果只想直接转换div或span等HTML元素 int您可以使用GrabzIt的Python库获得Word文档。 您必须通过 CSS选择器 您希望转换为HTML元素的 targetElement GrabzIt方法DOCXOptions 类。

...
<span id="Article">
<p>This is the content I am interested in.</p>
<img src="myimage.jpg">
</span>
...

在此示例中,我们希望捕获跨度中ID为的所有内容 Article,因此我们将其传递给GrabzIt API,如下所示。

from GrabzIt import GrabzItDOCXOptions
from GrabzIt import GrabzItClient

grabzIt = GrabzItClient.GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzItDOCXOptions.GrabzItDOCXOptions()
options.targetElement = "#Article"

grabzIt.URLToDOCX("http://www.bbc.co.uk/news", options)
# Then call the Save or SaveTo method
grabzIt.SaveTo("result.docx")