def tt(): # import os import winerror from win32com.client.dynamic import Dispatch, ERRORS_BAD_CONTEXT ERRORS_BAD_CONTEXT.append(winerror.E_NOTIMPL) my_dir = r"D:\AllDowns" my_pdf = "gpgp.pdf" os.chdir(my_dir) src = os.path.abspath(my_pdf) try: AvDoc = Dispatch("AcroExch.AVDoc") if AvDoc.Open(src, ""): pdDoc = AvDoc.GetPDDoc() jsObject = pdDoc.GetJSObject() jsObject.SaveAs(os.path.join(my_dir, 'output_example.jpeg'), "com.adobe.acrobat.jpeg") except Exception as e: print(str(e)) finally: AvDoc.Close(True) jsObject = None pdDoc = None AvDoc = None
def rm_rubric(pdffile): '''remove the pages associated with the rubric stored in the pdf ''' # if there is a rubric it is stored here rubricfile = get_info(pdffile, 'rubric') #print rubricfile, os.path.exists(rubricfile) if rubricfile == '': # no rubric stored. print 'no rubric found' return True # get number of pages in pdffile pddoc1 = pddoc pddoc1.Open(os.path.abspath(pdffile)) N1 = pddoc1.GetNumPages() pddoc2 = Dispatch("AcroExch.PDDoc") pddoc2.Open(os.path.abspath(rubricfile)) N2 = pddoc2.GetNumPages() #print N1, N2 pddoc1.DeletePages(N1 - N2, N1 - 1) set_info(pdffile, 'rubric', '') pddoc1.Save(1, os.path.abspath(pdffile)) pddoc1.Close() pddoc2.Close() return True
def add_rubric(pdffile, rubricfile, force=False): src1 = os.path.abspath(pdffile) src2 = os.path.abspath(rubricfile) # We do not want to add rubrics more than once if get_info(src1, 'rubric') == rubricfile and not force: return True pddoc1 = pddoc pddoc2 = Dispatch("AcroExch.PDDoc") pddoc1.Open(src1) N1 = pddoc1.GetNumPages() pddoc2.Open(src2) N2 = pddoc2.GetNumPages() # Insert rubric after last page of the other doc. pages start at 0 pddoc1.InsertPages(N1 - 1, pddoc2, 0, N2, 0) pddoc1.Save(1, src1) pddoc1.Close() pddoc2.Close() # store rubric so we know it is added set_info(pdffile, 'rubric', src2) return True
class AVDoc: def __init__(self, file, pddoc=None): if pddoc is None: self.avdoc = Dispatch("AcroExch.AVDoc") else: self.avdoc = pddoc self.file = file # Open # Opens a file. A new instance of AcroExch.AVDoc must be created for each displayed PDF file. # # Note:An application must explicitly close any AVDoc that it opens by calling AVDoc.Close (the destructor for the # AcroExch.AVDoc class does not call AVDoc.Close). # # Syntax # VARIANT_BOOL Open(BSTR szFullPath, BSTR szTempTitle); # # Parameters # szFullPath # # The full path of the file to open. # # szTempTitle # # An optional title for the window in which the file is opened. If szTempTitle is NULL or the empty string, # it is ignored. Otherwise, szTempTitle is used as the window title. # # Returns # -1 if the file was opened successfully, 0 otherwise. def open(self, temp_title=""): return self.avdoc.Open(self.file, temp_title) # Close # Closes a document. You can close all open AVDoc objects by calling App.CloseAllDocs. # # To reuse an AVDoc object, close it with AVDoc.Close, then use the AVDoc object’s LPDISPATCH for # AVDoc.OpenInWindow. # # Syntax # VARIANT_BOOL Close(long bNoSave); # # Parameters # bNoSave # # If a positive number, the document is closed without saving it. If 0 and the document has been modified, # the user is asked whether or not the file should be saved. # # Returns # Always returns -1, even if no document is open. def close_doc(self, no_save=0): return self.avdoc.Close(no_save)
def update_cyphy(version, upstream_rev): subprocess.check_call('git checkout {} ./CyPhyML.xme'.format(upstream_rev), shell=True) project = Dispatch("Mga.MgaProject") project.Create("MGA=" + "CyPhyML.mga", "MetaGME") import_xme(project, "CyPhyML.xme") do_mods(project, cyphy_mods) with in_tx(project): for lib in (l for l in project.RootFolder.ChildFolders if l.LibraryName): original_lib_relid = lib.RelID lib.RefreshLibrary(lib.LibraryName) lib = project.RootFolder.ChildObjectByRelID(original_lib_relid) import_xme(project, "CyPhyML-tonka.xme") with in_tx(project): project.RootFolder.GetObjectByPathDisp("/@Schematic|kind=SheetFolder/@EDACad|kind=ParadigmSheet/@CADModel|kind=ModelProxy").Referred = \ project.RootFolder.GetObjectByPathDisp("/@CAD|kind=SheetFolder/@3_SolidModeling|kind=ParadigmSheet/@CADModel|kind=Model") libs = [lib for lib in project.RootFolder.ChildFolders if lib.LibraryName] assert len(libs) == 2 libs.sort(key=lambda lib: lib.ID) original_lib, copy_lib = libs print(original_lib.AbsPath) print(copy_lib.AbsPath) switch_lib(from_=copy_lib, to=original_lib) copy_lib.DestroyObject() metaint_currentobj = project.RootFolder.GetObjectByPathDisp("/@Ports") project.Version = version project.Save() with in_tx(project): for fco in project.AllFCOs(project.CreateFilter()): if fco.ObjType == OBJTYPE_REFERENCE and fco.Referred is None: raise ValueError('Null reference {}'.format(fco.AbsPath)) metaint = Dispatch("MGA.Interpreter.MetaInterpreter") metaint.InvokeEx(project, metaint_currentobj, Dispatch("Mga.MgaFCOs"), 128) # launcher = win32com.client.DispatchEx("Mga.MgaLauncher") # launcher.RunComponent("MGA.Interpreter.MetaInterpreter", self.project, N) project.Save() dumper = Dispatch("Mga.MgaDumper") dumper.DumpProject(project, "CyPhyML.xme") project.Close(True)
def update_core(upstream_rev): subprocess.check_call('git checkout {} CyPhyML-core.xme'.format(upstream_rev), shell=True) project = Dispatch("Mga.MgaProject") project.Create("MGA=" + "CyPhyML-core.mga", "MetaGME") import_xme(project, "CyPhyML-core.xme") do_mods(project, core_mods) project.Save() dumper = Dispatch("Mga.MgaDumper") dumper.DumpProject(project, "CyPhyML-core.xme") project.Close(True)
# Gets or sets the milliseconds from an AcroTime. # # Minute # # Gets or sets the minutes from an AcroTime. # # Month # # Gets or sets the month from an AcroTime. # # Second # # Gets or sets the seconds from an AcroTime. # # Year # # Gets or sets the year from an AcroTime. sub_tipi.append(v.GetSubtype()) baslık.append(v.GetTitle()) obj_kordinat.append(kordinat) print(içindeki) print(kordinat) print(index) # Açılan dosyayı kapatmak için: dosya_kapandımı = pddoc.Close() # VARIANT_BOOL Close(); del pddoc print("son")
class PDDoc: def __init__(self, file): self.pddoc = Dispatch("AcroExch.PDDoc") self.file = file # self.dosya = os.path.abspath(dosya) # dosya ismi yolu ile birlikte tamamem olması gerekmektedir. # Open # Opens a file. A new instance of AcroExch.PDDoc must be created for each open PDF file. # # Syntax # VARIANT_BOOL Open(BSTR szFullPath); # # Parameters # szFullPath # # Full path of the file to be opened. # # Returns # -1 if the document was opened successfully, 0 otherwise. def open(self): return self.pddoc.Open(self.file) # Close # Closes a file. # # Note:If PDDoc and AVDoc are constructed with the same file, PDDoc.Close destroys both objects # (which closes the document in the viewer). # # Syntax # VARIANT_BOOL Close(); # # Returns # -1 if the document was closed successfully, 0 otherwise. def close(self): return self.pddoc.Close() # OpenAVDoc # Opens a window and displays the document in it. # # Syntax # LPDISPATCH OpenAVDoc(BSTR szTitle); # # Parameters # szTitle # # The title to be used for the window. A default title is used if szTitle is NULL or an empty string. # # Returns # The LPDISPATCH for the AcroExch.AVDoc that was opened, or NULL if the open fails. # OpenAVDoc ile açılan dosya yine AVDoc.Close() yöntemi ile kapatılmalıdır. def open_avdoc(self, title=""): avdoc = AcroExchAVDoc.AVDoc(self.file, pddoc=self.pddoc.OpenAVDoc(title)) return avdoc # GetFileName # Gets the name of the file associated with this AcroExch.PDDoc. # # Syntax # BSTR GetFileName(); # # Returns # The file name, which can currently contain up to 256 characters. def get_file_name(self): return self.pddoc.GetFileName() # Save # Saves a document. # # Syntax # VARIANT_BOOL Save(short nType, BSTR szFullPath); # # Parameters # nType # # Specifies the way in which the file should be saved. # # nType is a logical OR of one or more of the following flags: # # 0 = PDSaveIncremental — Write changes only, not the complete file. This will always result in a larger file, # even if objects have been deleted. # # 1 = PDSaveFull — Write the entire file to the filename specified by szFullPath. # # 2 = PDSaveCopy — Write a copy of the file into the file specified by szFullPath, but keep using the old file. # This flag can only be specified if PDSaveFull is also used. # # 3 = PDSaveCollectGarbage — Remove unreferenced objects; this often reduces the file size, and its usage is # encouraged. This flag can only be specified if PDSaveFull is also used. # # 4 = PDSaveLinearized — Save the file optimized for the web, providing hint tables. This allows the PDF file to be # byte-served. This flag can only be specified if PDSaveFull is also used. # # Note:If you save a file optimized for the web using the PDSaveLinearized flag, you must follow this sequence: # # 1. Open the PDF file with PDDoc.Open. # # 2. Call PDDoc.Save using the PDSaveLinearized flag. # # 3. Call PDDoc.Close. # # This allows batch optimization of files. # # szFullPath # # The new path to the file, if any. # # Returns # -1 if the document was successfully saved. Returns 0 if it was not or if the Acrobat application does not support # editing. # kayıt tipi çalışiyor mu ?????? def save(self, type, full_path=""): return self.pddoc.Save(type, full_path) # GetFlags # Gets a document’s flags. The flags indicate whether the document has been modified, whether the document is a # temporary document and should be deleted when closed, and the version of PDF used in the file. # # Syntax # long GetFlags(); # # Returns # The document’s flags, containing an OR of the following: # # Flag ## Description # # PDDocNeedsSave ## Document has been modified and needs to be saved. # # PDDocRequiresFullSave ## Document cannot be saved incrementally; it must be written using PDSaveFull. # # PDDocIsModified ## Document has been modified slightly (such as bookmarks or text annotations have been # opened or closed),but not in a way that warrants saving. # # PDDocDeleteOnClose ## Document is based on a temporary file that must be deleted when the document is # closed or saved. # # PDDocWasRepaired ## Document was repaired when it was opened. # # PDDocNewMajorVersion ## Document’s major version is newer than current. # # PDDocNewMinorVersion ## Document’s minor version is newer than current. # # PDDocOldVersion ## Document’s version is older than current. # # PDDocSuppressErrors ## Don’t display errors. # 8325 : Dökümanda bir değişiklik olduysa # 8320 : döküman kayıt edildikten sonra # 15 : yeni bir döküman oluşturulduğunda içinde sayfa yok. # 9344 : ??????????????????????????? def get_flags(self): return self.pddoc.GetFlags() # ClearFlags # Clears a document’s flags. The flags indicate whether the document has been modified, whether the document is a # temporary document and should be deleted when closed, and the version of PDF used in the file. This method can be # used only to clear, not to set, the flag bits. # # Syntax # VARIANT_BOOL ClearFlags(long nFlags); # # Parameters # nFlags # # Flags to be cleared. See PDDoc.GetFlags for a description of the flags. The flags PDDocWasRepaired, # PDDocNewMajorVersion, PDDocNewMinorVersion, and PDDocOldVersion are read-only and cannot be cleared. # # Returns # Always returns -1. def clear_flags(self, flags=0): return self.pddoc.ClearFlags(flags) # SetFlags # Sets a document’s flags indicating whether the document has been modified, whether the document is a temporary # document and should be deleted when closed, and the version of PDF used in the file. This method can be used # only to set, not to clear, the flag bits. # # Syntax # VARIANT_BOOL SetFlags(long nFlags); # # Parameters # nFlags # # Flags to be set. See PDDoc.GetFlags for a description of the flags. The flags PDDocWasRepaired, # PDDocNewMajorVersion, PDDocNewMinorVersion, and PDDocOldVersion are read-only and cannot be set. # 8320 dosya kapalı iken. # 8325 dosyada bir değişiklik olduysa # # Returns # Always returns -1. def set_flags(self, flags=0): return self.pddoc.SetFlags(flags) # GetInfo # Gets the value of a specified key in the document’s Info dictionary. A maximum of 512 bytes are returned. # # Syntax # BSTR GetInfo(BSTR szInfoKey); # # Parameters # szInfoKey # # The key whose value is obtained. # # Returns # The string if the value was read successfully. Returns an empty string if the key does not exist or its value # cannot be read. (Title, Author, Subject, Keywords, Creator, Producer, CreationDate, ModDate, Trapped) # http://pdf-file.nnn2.com/?p=103 def get_info(self, info): # info tobe numeric or text return self.pddoc.GetInfo(info) # SetInfo # Sets the value of a key in a document’s Info dictionary. # # Syntax # VARIANT_BOOL SetInfo(BSTR szInfoKey, BSTR szBuffer); # # Parameters # szInfoKey # # The key whose value is set. # # szBuffer # # The value to be assigned to the key. # # Returns # -1 if the value was added successfully, 0 if it was not or if the Acrobat application does not support editing. # belge özelliklerinde Özel sekmesinde "Özel özellikler" ekleme def set_info(self, info_key, value): return self.pddoc.SetInfo(info_key, value) # GetInstanceID # Gets the instance ID (the second element) from the ID array in the document’s trailer. # # Syntax # BSTR GetInstanceID(); # # Returns # A string whose maximum length is 32 characters, containing the document’s instance ID. def get_instance_id(self): return self.pddoc.GetInstanceID() # GetPermanentID # Gets the permanent ID (the first element) from the ID array in the document’s trailer. # # Syntax # BSTR GetPermanentID(); # # Returns # A string whose maximum length is 32 characters, containing the document’s permanent ID. def get_permanent_id(self): return self.pddoc.GetPermanentID() # GetJSObject # Gets a dual interface to the JavaScript object associated with the PDDoc. This allows automation clients full # access to both built-in and user-defined JavaScript methods available in the document. For more information on # working with JavaScript, see Developing Applications Using Interapplication Communication. # # Syntax # LDispatch* GetJSObject(); # # Returns # The interface to the JavaScript object if the call succeeded, NULL otherwise. # Burada adobe DOC objesi JavaScript komutlarını çalıştırmak için bir obje oluşturulur. # https://stackoverflow.com/questions/9383307/not-implemented-exception-when-using-pywin32-to-control-adobe-acrobat def get_js_object(self): docjava = AcroExchDocJava.DocJava(self.pddoc.GetJSObject()) return docjava # GetNumPages # Gets the number of pages in a file. # # Syntax # long GetNumPages(); # # Returns # The number of pages, or -1 if the number of pages cannot be determined. # sayfa sayısını döndürür def get_num_pages(self): return self.pddoc.GetNumPages() # AcquirePage # Acquires the specified page. # # Syntax # LPDISPATCH AcquirePage(long nPage); # # Parameters # nPage # # The number of the page to acquire. The first page is page 0. # # Returns # The LPDISPATCH for the AcroExch.PDPage object for the acquired page. # Returns NULL if the page could not be acquired. # herzaman ilk sayfa 0 # Burada sayfa objesi alındıktan sonra AcroExch.PDPage geçerlidir def acquire_page(self, page_number=0): pdpage = AcroExchPDPage.PDPage(self.pddoc.AcquirePage(page_number)) return pdpage # GetPageMode # Gets a value indicating whether the Acrobat application is currently displaying only pages, pages and thumbnails, # or pages and bookmarks. # # Syntax # long GetPageMode(); # # Returns # The current page mode. Will be one of the following values: # # PDDontCare: 0 — leave the view mode as it is # # PDUseNone: 1 — display without bookmarks or thumbnails # # PDUseThumbs: 2 — display using thumbnails # # PDUseBookmarks: 3 — display using bookmarks # # PDFullScreen: 4 — display in full screen mode # First argument (long nPageMode): Value of # PDF initial page display mode # 0: Page only # 1: Page only * # 2: Thumbnail image panel and page * # 3: Bookmark panel and Page * # 4: Open in full screen mode. <Caution> # It is reflected as 5: 1. # 6: Layers panel and page * # 7: Attachment panel and page * # 8: Ignored after this value def get_page_mode(self): return self.pddoc.GetPageMode() # SetPageMode # Sets the page mode in which a document is to be opened: display only pages, pages and thumbnails, or pages and # bookmarks. # # Syntax # VARIANT_BOOL SetPageMode(long nPageMode); # # Parameters # nPageMode # # The page mode to be set. Possible values: # # PDDontCare: 0 — leave the view mode as it is # # PDUseNone: 1 — display without bookmarks or thumbnails # # PDUseThumbs: 2 — display using thumbnails # # PDUseBookmarks: 3 — display using bookmarks def set_page_mode(self, page_mode_value): return self.pddoc.SetPageMode(page_mode_value) # Create # Creates a new AcroExch.PDDoc. # # Syntax # VARIANT_BOOL Create(); # # Returns # -1 if the document is created successfully, 0 if it is not or if the Acrobat application does not support editing. # yenibir döküman oluşturmak için def create(self): return self.pddoc.Create() # CreateThumbs # Creates thumbnail images for the specified page range in a document. # # Syntax # VARIANT_BOOL CreateThumbs(long nFirstPage, long nLastPage); # # Parameters # nFirstPage # # First page for which thumbnail images are created. The first page in a PDDoc object is page 0. # # nLastPage # # Last page for which thumbnail images are created. # # Returns # -1 if thumbnail images were created successfully, 0 if they were not or if the Acrobat application # does not support editing. def create_thumbs(self, start_page, end_page): return self.pddoc.CreateThumbs(start_page, end_page) # DeleteThumbs # Deletes thumbnail images from the specified pages in a document. # # Syntax # VARIANT_BOOL DeleteThumbs(long nStartPage, long nEndPage); # # Parameters # nStartPage # # First page whose thumbnail image is deleted. The first page in a PDDoc object is page 0. # # nEndPage # # Last page whose thumbnail image is deleted. # # Returns # -1 if the thumbnails were deleted, 0 if they were not deleted or if the Acrobat application does not # support editing. def delete_thumbs(self, start_page, end_page): return self.pddoc.DeleteThumbs(start_page, end_page) # CropPages # Crops the pages in a specified range in a document. This method ignores the request if either the width # or height of the crop box is less than 72 points (one inch). # # Syntax # VARIANT_BOOL CropPages(long nStartPage, long nEndPage, # short nEvenOrOddPagesOnly, # LPDISPATCH iAcroRect); # # Parameters # nStartPage # # First page that is cropped. The first page in a PDDoc object is page 0. # # nEndPage # # Last page that is cropped. # # nEvenOrOddPagesOnly # # Value indicating which pages in the range are cropped. Must be one of the following: # # 0 — crop all pages in the range # # 1 — crop only odd pages in the range # # 2 — crop only even pages in the range # # iAcroRect # # An LPDISPATCH for a CAcroRect specifying the cropping rectangle, which is specified in user space. # # Returns # -1 if the pages were cropped successfully, 0 otherwise. # Burada iAcroRect objesi AcroExchRect sınıfı ile tanımlanması gerekiyor. def crop_pages(self, start_page=0, end_page=0, even_or_odd=0, iAcroRect=AcroExchRect.Rect().create_rect()): return self.pddoc.CropPages(start_page, end_page, even_or_odd, iAcroRect) # DeletePages # Deletes pages from a file. # # Syntax # VARIANT_BOOL DeletePages(long nStartPage, long nEndPage); # # Parameters # nStartPage # # The first page to be deleted. The first page in a PDDoc object is page 0. # # nEndPage # # The last page to be deleted. # # Returns # -1 if the pages were successfully deleted. Returns 0 if they were not or if the Acrobat application # does not support editing. def delete_pages(self, start_page, end_page): return self.pddoc.DeletePages(start_page, end_page) # InsertPages # Inserts the specified pages from the source document after the indicated page within the current document. # # Syntax # VARIANT_BOOL InsertPages(long nInsertPageAfter, # # LPDISPATCH iPDDocSource,long nStartPage, # # long nNumPages, long bBookmarks); # # Parameters # nInsertPageAfter # # The page in the current document after which pages from the source document are inserted. The first page in a # PDDoc object is page 0. # # iPDDocSource # # The LPDISPATCH for the AcroExch.PDDoc containing the pages to insert. iPDDocSource contains the instance # variable m_lpDispatch, which contains the LPDISPATCH. # # nStartPage # # The first page in iPDDocSource to be inserted into the current document. # # nNumPages # # The number of pages to be inserted. # # bBookmarks # # If a positive number, bookmarks are copied from the source document. If 0, they are not. # # Returns # -1 if the pages were successfully inserted. Returns 0 if they were not or if the Acrobat application does not # support editing. def insert_pages(self, page_after=0, added_PDDoc=None, start_page=0, pages=0, bookmarks=0): ek = PDDoc(added_PDDoc) ek.open() return self.pddoc.InsertPages(page_after, ek.pddoc, start_page, pages, bookmarks) # MovePage # Moves a page to another location within the same document. # # Syntax # VARIANT_BOOL MovePage(long nMoveAfterThisPage, # # long nPageToMove); # # Parameters # nMoveAfterThisPage # # The page being moved is placed after this page number. The first page in a PDDoc object is page 0. # # nPageToMove # # Page number of the page to be moved. # # Returns # 0 if the Acrobat application does not support editing, -1 otherwise. def move_page(self, page_after, page): return self.pddoc.MovePage(page_after, page) # ReplacePages # Replaces the indicated pages in the current document with those specified from the source document. # No links or bookmarks are copied from iPDDocSource, but text annotations may optionally be copied. # # Syntax # VARIANT_BOOL ReplacePages(long nStartPage, # # LPDISPATCH iPDDocSource, # long nStartSourcePage, long nNumPages, # long bMergeTextAnnotations); # # Parameters # nStartPage # # The first page within the source file to be replaced. The first page in a PDDoc object is page 0. # # iPDDocSource # # The LPDISPATCH for the AcroExch.PDDoc containing the new copies of pages that are replaced. # iPDDocSource contains the instance variable m_lpDispatch, which contains the LPDISPATCH. # # nStartSourcePage # # The first page in iPDDocSource to use as a replacement page. # # nNumPages # # The number of pages to be replaced. # # bMergeTextAnnotations # # If a positive number, text annotations from iPDDocSource are copied. If 0, they are not. # # Returns # -1 if the pages were successfully replaced. Returns 0 if they were not or if the Acrobat application does not # support editing. def replace_pages(self, start_page, iPDDocSource, source_start_page, source_pages, merge_text_annotations=1): re = PDDoc(iPDDocSource) re.open() return self.pddoc.ReplacePages(start_page, re.pddoc, source_start_page, source_pages, merge_text_annotations) # CreateTextSelect # Creates a text selection from the specified rectangle on the specified page. After creating the text selection, # use the AVDoc.SetTextSelection method to use it as the document’s selection, and use AVDoc.ShowTextSelect to show # the selection. # # Syntax # LPDISPATCH CreateTextSelect(long nPage, LPDISPATCH iAcroRect); # # Parameters # nPage # # The page on which the selection is created. The first page in a PDDoc object is page 0. # # iAcroRect # # The LPDISPATCH for the AcroExch.Rect enclosing the region to select. iAcroRect contains the instance # variable m_lpDispatch, which contains the LPDISPATCH. # # Returns # The LPDISPATCH for an AcroExch.PDTextSelect containing the text selection. Returns NULL if the text selection # was not created successfully. def create_text_select(self, page, acrorect): # TODO: future return self.pddoc.CreateTextSelect(page, acrorect)