Ajout de la possibilité de convertir du HTML ou des pages Web into Les documents Word de votre application n’ont jamais été aussi simples avec API Python de GrabzIt. Cependant, avant de commencer, rappelez-vous qu'après avoir appelé le URLToDOCX, HTMLToDOCX or FileToDOCX méthodes le Save or SaveTo La méthode doit être appelée pour créer le fichier DOCX.
La capture de pages Web au format DOCX convertit la page Web entière into un document Word pouvant comporter plusieurs pages. Un seul paramètre est requis pour convertir une page Web into un document Word ou à convertir HTML en DOCX comme indiqué dans les exemples ci-dessous.
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
Vous pouvez transmettre un identifiant personnalisé au DOCX Comme indiqué ci-dessous, cette valeur est ensuite renvoyée à votre gestionnaire GrabzIt Python. Par exemple, cet identifiant personnalisé pourrait être un identifiant de base de données, permettant d'associer un document DOCX à un enregistrement de base de données particulier.
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")
Pour ajouter un en-tête ou un pied de page à un document Word, vous pouvez demander à appliquer un programme particulier. modèle au DOCX en cours de génération. Ce modèle doit être saved à l'avance et spécifiera le contenu de l'en-tête et du pied de page avec toutes les variables spéciales. Dans l'exemple de code ci-dessous, l'utilisateur utilise un modèle qu'ils ont créé, appelé "mon modèle".
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")
Si vous voulez simplement convertir un élément HTML tel qu'un div ou un span directement into Un document Word que vous pouvez utiliser avec la bibliothèque Python de GrabzIt. Vous devez passer le Sélecteur CSS de l'élément HTML que vous souhaitez convertir en targetElement
méthode de GrabzItDOCXOptions classe.
... <span id="Article"> <p>This is the content I am interested in.</p> <img src="myimage.jpg"> </span> ...
Dans cet exemple, nous souhaitons capturer tout le contenu de la plage portant l’id de Article
Par conséquent, nous transmettons cela à l'API GrabzIt comme indiqué ci-dessous.
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")