ライブラリに移動して、MS Word ドキュメントを生成および編集します

ヘッダーとフッター、表と画像を Word DOCX ファイルに挿入するなど、一般的なワード処理タスクを管理および自動化するためのオープンソース Go ライブラリ。

unioffice は強力なオープン ソースの純粋な Go ライブラリであり、ソフトウェア開発者が Microsoft Word を必要とせずに、独自のアプリケーション内で Word ドキュメントを管理し、一般的なワープロ タスクを簡単に自動化できるようにします。ライブラリは非常に最適化されており、要件を満たすようにコードを簡単に編集できます。

unioffice ライブラリは、Office Open XML ドキュメントの生成、編集、書式設定、および処理に使用できる強力な Go ベースのライブラリです。このライブラリは、Word 文書の読み取り、書き込み、変更、テキストの書式設定のサポート、自動生成された目次、文書ページへの配置、ヘッダーとフッターの挿入、表の追加、文書をテンプレートとして開くなど、いくつかの重要なワープロ機能をサポートしています。 、フォーム フィールドのサポートなど。

Previous Next

ユニオフィスを始めよう

unioffice をプロジェクトにインストールする推奨される方法は、GitHub を使用することです。スムーズなインストールのために、次のコマンドを使用してください。

GitHub 経由で unioffice をインストールする

go get github.com/unidoc/unioffice/
go build -i github.com/unidoc/unioffice/...   

Go API を介して Word DOCX ドキュメントを作成する

オープン ソース ライブラリ unioffice は、新しい Word DOCX ドキュメントを簡単に作成するための機能を提供しています。独自のアプリケーション内で既存の Microsoft Word 文書を簡単に開いて変更することもできます。ライブラリには、新しいテキスト パラグラフの追加、ページへの画像の挿入、テキストの配置、ヘッダーとフッターの追加、表の追加などの機能も含まれていました。

Go API を介して Word 文書のプロパティにアクセスする

func main() {
	doc, err := document.Open("document.docx")
	if err != nil {
		log.Fatalf("error opening document: %s", err)
	}
	defer doc.Close()
	cp := doc.GetOrCreateCustomProperties()
	// You can read properties from the document
	fmt.Println("AppVersion", *cp.GetPropertyByName("AppVersion").X().Lpwstr)
	fmt.Println("Company", *cp.GetPropertyByName("Company").X().Lpwstr)
	fmt.Println("DocSecurity", *cp.GetPropertyByName("DocSecurity").X().I4)
	fmt.Println("LinksUpToDate", *cp.GetPropertyByName("LinksUpToDate").X().Bool)
	fmt.Println("Non-existent", cp.GetPropertyByName("nonexistentproperty"))
	// And change them as well
	cp.SetPropertyAsLpwstr("Company", "Another company") // text, existing property
	fmt.Println("Company", *cp.GetPropertyByName("Company").X().Lpwstr)
	// Adding new properties
	cp.SetPropertyAsLpwstr("Another text property", "My text value") // text
	cp.SetPropertyAsI4("Another integer number property", 42)        // int32
	cp.SetPropertyAsR8("Another float number property", 3.14)        // float64
	cp.SetPropertyAsDate("Another date property", time.Now())        // date
	doc.SaveToFile("document_customized.docx")
	// For new documents all is the same
	docNew := document.New()
	defer docNew.Close()
	cpNew := docNew.GetOrCreateCustomProperties()
	cpNew.SetPropertyAsLpwstr("Another text property", "My text value") // text
	cpNew.SetPropertyAsI4("Another integer number property", 42)        // int23
	cpNew.SetPropertyAsR8("Another float number property", 3.14)        // float64
	cpNew.SetPropertyAsDate("Another date property", time.Now())        // date
	docNew.SaveToFile("document_new.docx")
}
  

 Word DOCX ファイルに画像を挿入する

オープン ソース ライブラリ unioffice は、ソフトウェア開発者が Microsoft Word ドキュメント内で画像を使用できるようにします。選択した場所への画像の挿入、既存の画像の変更、画像のテキストの折り返し、画像の削除などの機能をサポートしています。画像を追加するには、画像の名前と場所を指定する必要があります。

Go APIでWord文書で画像を管理

func main() {
	doc := document.New()
	defer doc.Close()
	img1, err := common.ImageFromFile("gophercolor.png")
	if err != nil {
		log.Fatalf("unable to create image: %s", err)
	}
	img2data, err := ioutil.ReadFile("gophercolor.png")
	if err != nil {
		log.Fatalf("unable to read file: %s", err)
	}
	img2, err := common.ImageFromBytes(img2data)
	if err != nil {
		log.Fatalf("unable to create image: %s", err)
	}
	img1ref, err := doc.AddImage(img1)
	if err != nil {
		log.Fatalf("unable to add image to document: %s", err)
	}
	img2ref, err := doc.AddImage(img2)
	if err != nil {
		log.Fatalf("unable to add image to document: %s", err)
	}
	para := doc.AddParagraph()
	anchored, err := para.AddRun().AddDrawingAnchored(img1ref)
	if err != nil {
		log.Fatalf("unable to add anchored image: %s", err)
	}
	anchored.SetName("Gopher")
	anchored.SetSize(2*measurement.Inch, 2*measurement.Inch)
	anchored.SetOrigin(wml.WdST_RelFromHPage, wml.WdST_RelFromVTopMargin)
	anchored.SetHAlignment(wml.WdST_AlignHCenter)
	anchored.SetYOffset(3 * measurement.Inch)
	anchored.SetTextWrapSquare(wml.WdST_WrapTextBothSides)
	run := para.AddRun()
	for i := 0; i < 16; i++ {
		run.AddText(lorem)
		// drop an inline image in
		if i == 13 {
			inl, err := run.AddDrawingInline(img1ref)
			if err != nil {
				log.Fatalf("unable to add inline image: %s", err)
			}
			inl.SetSize(1*measurement.Inch, 1*measurement.Inch)
		}
		if i == 15 {
			inl, err := run.AddDrawingInline(img2ref)
			if err != nil {
				log.Fatalf("unable to add inline image: %s", err)
			}
			inl.SetSize(1*measurement.Inch, 1*measurement.Inch)
		}
	}
	doc.SaveToFile("image.docx")
}
  

Word 文書にヘッダーとフッターを追加する

ヘッダーとフッターを使用して、作成者名、ドキュメント タイトル、ページ番号など、ユーザーがドキュメントのすべてのページに表示したい情報を含めることができます。 unioffice ライブラリを使用すると、ソフトウェア開発者は Word 文書にヘッダーとフッターを簡単に追加できます。また、ドキュメント セクションに応じて異なるヘッダーとフッターを使用することもできます。また、偶数、奇数、および最初の機能のサポートも含まれていました。

ヘッダーとフッターをGo APIでWord文書に追加

func main() {
	doc := document.New()
	defer doc.Close()
	img, err := common.ImageFromFile("gophercolor.png")
	if err != nil {
		log.Fatalf("unable to create image: %s", err)
	}
	hdr := doc.AddHeader()
	// We need to add a reference of the image to the header instead of to the
	// document
	iref, err := hdr.AddImage(img)
	if err != nil {
		log.Fatalf("unable to to add image to document: %s", err)
	}
	para := hdr.AddParagraph()
	para.Properties().AddTabStop(2.5*measurement.Inch, wml.ST_TabJcCenter, wml.ST_TabTlcNone)
	run := para.AddRun()
	run.AddTab()
	run.AddText("My Document Title")
	imgInl, _ := para.AddRun().AddDrawingInline(iref)
	imgInl.SetSize(1*measurement.Inch, 1*measurement.Inch)
	// Headers and footers are not immediately associated with a document as a
	// document can have multiple headers and footers for different sections.
	doc.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault)
	ftr := doc.AddFooter()
	para = ftr.AddParagraph()
	para.Properties().AddTabStop(6*measurement.Inch, wml.ST_TabJcRight, wml.ST_TabTlcNone)
	run = para.AddRun()
	run.AddText("Some subtitle goes here")
	run.AddTab()
	run.AddText("Pg ")
	run.AddField(document.FieldCurrentPage)
	run.AddText(" of ")
	run.AddField(document.FieldNumberOfPages)
	doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrDefault)
	lorem := `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lobortis, lectus dictum feugiat tempus, sem neque finibus enim, sed eleifend sem nunc ac diam. Vestibulum tempus sagittis elementum`
	for i := 0; i < 5; i++ {
		para = doc.AddParagraph()
		run = para.AddRun()
		run.AddText(lorem)
	}
	doc.SaveToFile("header-footer.docx")
}
  

Word DOCX で表を操作する

オープン ソース ライブラリ unioffice を使用すると、コンピューター プログラマーは Word ドキュメント内のテーブルを追加および変更できます。表は非常に便利で、データをより適切に整理して表示するために使用できます。ボーダーの有無にかかわらずテーブルの追加をサポートし、テーブル スタイルを簡単に作成できます。テーブルにコンテンツを簡単に配置し、必要に応じてサイズを調整できます。

 日本