I'd probably start by creating a test Word document and adding a source 
manually in it from the Word GUI.

Then, I would likely want to do something like this in python.

# Sample python to list the bibliography entries
import win32com.client


wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")

wordapp.Visible = True

doc = wordapp.Documents.Open(r"C:\\temp\\testing.docx")

for source in doc.Bibliography.Sources:
    print("source: " + source.XML)
    # Now save the XML as a variable:
    myxml = source.XML

wordapp.Quit()

That should give you the properly formatted source XML that Word is looking 
for....  And you can then modify a known working version of the XML so you can 
add all your bibliographical information in Word.

My test (using the above process) showed up like this:

<b:Source 
xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography";><b:Tag>Ste20</b:Tag><b:SourceType>Book</b:SourceType><b:Guid>{92931D8B-470B-4359-A4B8-3C53859A1B3F}</b:Guid><b:Author><b:Author><b:NameList><b:Person><b:Last>Manross</b:Last><b:First>Steven</b:First></b:Person></b:NameList></b:Author></b:Author><b:Title>How
 To Add A Bibliography Entry</b:Title><b:Year>2023</b:Year><b:City>Nowhere, AZ< 
/b:City><b:Publisher>Some Really Good 
Publisher</b:Publisher><b:RefOrder>1</b:RefOrder></b:Source>


# NOW use the Word GUI to Delete the source from the Bibliography info then 
save the document using the GUI

And then you can use the code above to verify there are no more sources:

And last but not least, re-add your source info programmatically:

import win32com.client


wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")

wordapp.Visible = True

doc = wordapp.Documents.Open(r"C:\\temp\\testing.docx")

myxml = '<b:Source 
xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography";><b:Tag>Ste20</b:Tag><b:SourceType>Book</b:SourceType><b:Guid>{92931D8B-470B-4359-A4B8-3C53859A1B3F}</b:Guid><b:Author><b:Author><b:NameList><b:Person><b:Last>Manross</b:Last><b:First>Steven</b:First></b:Person></b:NameList></b:Author></b:Author><b:Title>How
 To Add A Bibliography Entry</b:Title><b:Year>2023</b:Year><b:City>Nowhere, AZ< 
/b:City><b:Publisher>Some Really Good 
Publisher</b:Publisher><b:RefOrder>1</b:RefOrder></b:Source>'

doc.Bibliography.Sources.Add(myxml)

doc.Save()
wordapp.Quit()


I'm pretty sure that your Bibliography XML is likely not formatted the way Word 
wants it..  Microsoft likes their own formats on everything.

If It matters, I tested this using Word 2019 but it should be backwards 
compatible all the way back to 2007.

HTH and Enjoy

P.S. This is not something that is limited to Python..  It can be done in just 
about any language that can access COM objects...  VBScript, Perl, Python, etc 
and there are tons of references on using VB and or VBScript macros that I 
found (but not much on exactly what the source XML looked like)

From: python-win32 <python-win32-bounces+steven=manross....@python.org> On 
Behalf Of Ryan Dikdan
Sent: Tuesday, February 28, 2023 11:35 PM
To: python-win32@python.org
Subject: [python-win32] How to add citations to a word document

Hello, I'm not sure how this mailing list works for support, but I'm trying to 
make a python script that formats downloaded citations and inserts them into 
word (avoiding all the hassle of external citation managers). I found pywin32 
as the only way, but the documentation on doc.Bibliography.Sources.Add is 
scarce, so I was wondering if you all could help me. I have properly formatted 
xml of sources (since I could upload it to the source manager successfully, 
even though I think it's encoded in utf8 bom or sig or something). But whenever 
I put either the string of that content or the filename into the above Add() 
command in python it says the XML isn't formatted properly. I'd appreciate any 
help. Thanks!
_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to