{"id":18842034,"url":"https://github.com/odedd/docx","last_synced_at":"2025-10-04T12:34:21.449Z","repository":{"id":223438647,"uuid":"117348077","full_name":"odedd/docx","owner":"odedd","description":null,"archived":false,"fork":false,"pushed_at":"2021-06-13T10:02:37.000Z","size":330,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-30T10:51:46.340Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/odedd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-01-13T14:01:39.000Z","updated_at":"2021-06-13T10:02:39.000Z","dependencies_parsed_at":"2024-02-20T08:54:24.015Z","dependency_job_id":null,"html_url":"https://github.com/odedd/docx","commit_stats":null,"previous_names":["odedd/docx"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odedd%2Fdocx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odedd%2Fdocx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odedd%2Fdocx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/odedd%2Fdocx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/odedd","download_url":"https://codeload.github.com/odedd/docx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239780120,"owners_count":19695734,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T02:53:24.428Z","updated_at":"2025-10-04T12:34:16.400Z","avatar_url":"https://github.com/odedd.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# docx\r\n\r\nA ruby library/gem for interacting with `.docx` files. currently capabilities include reading paragraphs/bookmarks, inserting text at bookmarks, reading tables/rows/columns/cells and saving the document.\r\n\r\n## Usage\r\n\r\n### Install\r\n\r\nRequires ruby (tested with 2.1.1)\r\n\r\n    gem 'docx', '~\u003e 0.2.07', :require =\u003e [\"docx\"]\r\n\r\n### Reading\r\n\r\n``` ruby\r\nrequire 'docx'\r\n\r\n# Create a Docx::Document object for our existing docx file\r\ndoc = Docx::Document.open('example.docx')\r\n\r\n# Retrieve and display paragraphs\r\ndoc.paragraphs.each do |p|\r\n  puts p\r\nend\r\n\r\n# Retrieve and display bookmarks, returned as hash with bookmark names as keys and objects as values\r\ndoc.bookmarks.each_pair do |bookmark_name, bookmark_object|\r\n  puts bookmark_name\r\nend\r\n```\r\n\r\n### Rendering html\r\n``` ruby\r\nrequire 'docx'\r\n\r\n# Retrieve and display paragraphs as html\r\ndoc = Docx::Document.open('example.docx')\r\ndoc.paragraphs.each do |p|\r\n  puts p.to_html\r\nend\r\n```\r\n\r\n### Reading tables\r\n\r\n``` ruby\r\nrequire 'docx'\r\n\r\n# Create a Docx::Document object for our existing docx file\r\ndoc = Docx::Document.open('tables.docx')\r\n\r\nfirst_table = doc.tables[0]\r\nputs first_table.row_count\r\nputs first_table.column_count\r\nputs first_table.rows[0].cells[0].text\r\nputs first_table.columns[0].cells[0].text\r\n\r\n# Iterate through tables\r\ndoc.tables.each do |table|\r\n  table.rows.each do |row| # Row-based iteration\r\n    row.cells.each do |cell|\r\n      puts cell.text\r\n    end\r\n  end\r\n  \r\n  table.columns.each do |column| # Column-based iteration\r\n    column.cells.each do |cell|\r\n      puts cell.text\r\n    end\r\n  end\r\nend\r\n```\r\n\r\n### Writing\r\n\r\n``` ruby\r\nrequire 'docx'\r\n\r\n# Create a Docx::Document object for our existing docx file\r\ndoc = Docx::Document.open('example.docx')\r\n\r\n# Insert a single line of text after one of our bookmarks\r\ndoc.bookmarks['example_bookmark'].insert_text_after(\"Hello world.\")\r\n\r\n# Insert multiple lines of text at our bookmark\r\ndoc.bookmarks['example_bookmark_2'].insert_multiple_lines_after(['Hello', 'World', 'foo'])\r\n\r\n# Remove paragraphs\r\ndoc.paragraphs.each do |p|\r\n  p.remove! if p.to_s =~ /TODO/\r\nend\r\n\r\n# Save document to specified path\r\ndoc.save('example-edited.docx')\r\n```\r\n\r\n### Advanced\r\n\r\n``` ruby\r\nrequire 'docx'\r\n\r\nd = Docx::Document.open('example.docx')\r\n\r\n# The Nokogiri::XML::Node on which an element is based can be accessed using #node\r\nd.paragraphs.each do |p|\r\n  puts p.node.inspect\r\nend\r\n\r\n# The #xpath and #at_xpath methods are delegated to the node from the element, saving a step\r\np_element = d.paragraphs.first\r\np_children = p_element.xpath(\"//child::*\") # selects all children\r\np_child = p_element.at_xpath(\"//child::*\") # selects first child\r\n```\r\n\r\n## Development\r\n\r\n### todo\r\n\r\n* Calculate element formatting based on values present in element properties as well as properties inherited from parents\r\n* Default formatting of inserted elements to inherited values\r\n* Implement formattable elements.\r\n* Implement styles.\r\n* Easier multi-line text insertion at a single bookmark (inserting paragraph nodes after the one containing the bookmark)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fodedd%2Fdocx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fodedd%2Fdocx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fodedd%2Fdocx/lists"}