[python-win32] How to add citations to a word document

Steven Manross steven at manross.net
Fri Mar 17 02:04:41 EDT 2023


A quick google of your error led me to this, although I am not 100% sure it will solve your issue… however, it is worth trying at a minimum.

https://stackoverflow.com/questions/17594211/you-are-not-allowed-to-edit-this-selection-because-it-is-protected-but-only-s

They suggest changing the View and explain that Microsoft blocks certain thing in Reading Layout.

This linked website also has some comments about changing security settings of the document in the Trust Center which may also be of some help, but I did not validate or test their suggestions.

Good luck, as I did not have this problem (likely because I started from a blank document).

Steven
From: Ryan Dikdan <rjd254 at njms.rutgers.edu>
Sent: Thursday, March 16, 2023 10:12 PM
To: Steven Manross <steven at manross.net>; python-win32 at python.org
Subject: Re: How to add citations to a word document

Thank you!! I've made a lot of progress. I've been able to reformat citations, but I've hit another roadbump that chat gpt can't help me with anymore. I successfully have a script that can take a citation file and load it into word and automatically place a citation, but if I want to put a citation on top of an existing citation Field, it says that the field is protected. I can't find a way to unprotect it, either through the selection or the document itself. Here's my code:

`word = win32.GetActiveObject("Word.Application")

doc = word.ActiveDocument

doc.Bibliography.Sources.Add(xml_string)

cursor_pos = word.Selection.Range.Start

fields = doc.Fields
current_field = None
for field in fields:
    if cursor_pos >= field.Result.Start and cursor_pos <= field.Result.End:
        current_field = field
        break

if current_field is not None:
    if current_field.Type == win32.constants.wdFieldCitation:
        current_code = current_field.Code.Text
        # \\m<file://m> is just used to show that there are multiple sources in this one citation.
        current_field.Code.Text = current_code + f'\\m {tag}'
else:
    # \\l<file://l> 1033 just says means that it's in english
    current_field = doc.Range(cursor_pos, cursor_pos).Fields.Add(doc.Range(cursor_pos, cursor_pos), win32.constants.wdFieldCitation, Text=f" {tag} \\l<file://l> 1033", PreserveFormatting=True)

current_field.Update()`

But this gives the error pywintypes.com_error:
(-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'You are not allowed to edit this selection because it is protected.', 'wdmain11.chm', 25604, -2146822164), None)​

when the cursor is inside of another field. Any other way to edit or unprotect the field would be appreciated. I was thinking of deleting and remaking the field, but this duplicates the references I think.


     Yours truly,
          Ryan Dikdan
________________________________
From: Steven Manross <steven at manross.net<mailto:steven at manross.net>>
Sent: Wednesday, March 1, 2023 6:43 PM
To: Ryan Dikdan <rjd254 at njms.rutgers.edu<mailto:rjd254 at njms.rutgers.edu>>; python-win32 at python.org<mailto:python-win32 at python.org> <python-win32 at python.org<mailto:python-win32 at python.org>>
Subject: RE: How to add citations to a word document

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="https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Fbibliography&data=05%7C01%7Crjd254%40njms.rutgers.edu%7C1eb7c0945071410f7a2c08db1aaeb821%7Cb92d2b234d35447093ff69aca6632ffe%7C1%7C0%7C638133110076478769%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=AsqVPr1onGfezeYnBY4NpCgC3PZBfeVTqHO8Ird%2FdTs%3D&reserved=0"><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<https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Fbibliography&data=05%7C01%7Crjd254%40njms.rutgers.edu%7C1eb7c0945071410f7a2c08db1aaeb821%7Cb92d2b234d35447093ff69aca6632ffe%7C1%7C0%7C638133110076478769%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=AsqVPr1onGfezeYnBY4NpCgC3PZBfeVTqHO8Ird%2FdTs%3D&reserved=0%22%3e%3cb:Tag%3eSte20%3c/b:Tag%3e%3cb:SourceType%3eBook%3c/b:SourceType%3e%3cb:Guid%3e%7b92931D8B-470B-4359-A4B8-3C53859A1B3F%7d%3c/b:Guid%3e%3cb:Author%3e%3cb:Author%3e%3cb:NameList%3e%3cb:Person%3e%3cb:Last%3eManross%3c/b:Last%3e%3cb:First%3eSteven%3c/b:First%3e%3c/b:Person%3e%3c/b:NameList%3e%3c/b:Author%3e%3c/b:Author%3e%3cb: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="https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Fbibliography&data=05%7C01%7Crjd254%40njms.rutgers.edu%7C1eb7c0945071410f7a2c08db1aaeb821%7Cb92d2b234d35447093ff69aca6632ffe%7C1%7C0%7C638133110076478769%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=AsqVPr1onGfezeYnBY4NpCgC3PZBfeVTqHO8Ird%2FdTs%3D&reserved=0"><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<https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Fbibliography&data=05%7C01%7Crjd254%40njms.rutgers.edu%7C1eb7c0945071410f7a2c08db1aaeb821%7Cb92d2b234d35447093ff69aca6632ffe%7C1%7C0%7C638133110076478769%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=AsqVPr1onGfezeYnBY4NpCgC3PZBfeVTqHO8Ird%2FdTs%3D&reserved=0%22%3e%3cb:Tag%3eSte20%3c/b:Tag%3e%3cb:SourceType%3eBook%3c/b:SourceType%3e%3cb:Guid%3e%7b92931D8B-470B-4359-A4B8-3C53859A1B3F%7d%3c/b:Guid%3e%3cb:Author%3e%3cb:Author%3e%3cb:NameList%3e%3cb:Person%3e%3cb:Last%3eManross%3c/b:Last%3e%3cb:First%3eSteven%3c/b:First%3e%3c/b:Person%3e%3c/b:NameList%3e%3c/b:Author%3e%3c/b:Author%3e%3cb: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.net at python.org<mailto:python-win32-bounces+steven=manross.net at python.org>> On Behalf Of Ryan Dikdan
Sent: Tuesday, February 28, 2023 11:35 PM
To: python-win32 at python.org<mailto:python-win32 at 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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/python-win32/attachments/20230317/30422f67/attachment-0001.html>


More information about the python-win32 mailing list